## HW 2 Example R code: library(mdsr) library(tidyverse) #Problem 1, Chapter 3 Exercises: library(babynames) BabynamesDist <- make_babynames_dist() #BabynamesDist ## creating a data frame called 'angelica' with only the female Angelicas: angelica <- BabynamesDist %>% filter(name == "Angelica" & sex == "F") name_plot <- ggplot(data = angelica, aes(x = year)) # basic ggplot object (with x-axis) to build on ## adding line plot with proportions of Angelicas BORN each year. name_plot <- name_plot + geom_line(aes(y = prop), size = 2) ## Adding a y-axis label name_plot <- name_plot + ylab("Proportion of People") + xlab(NULL) name_plot #Problem 2, Chapter 3 Exercises: install.packages("nasaweather") library(nasaweather) # Simple scatterplot using geom_point g <- ggplot(data = storms, aes(y = wind, x = pressure)) g + geom_point(size = 3) # Addition of information on a third variable (categorical) using the Color aesthetic g + geom_point(aes(color = type), size = 3) #Problem 3, Chapter 3 Exercises: library(mosaicData) head(Marriage) # (a) ## ## boxplot of ggplot( data = Marriage, aes( x = race, y = age ) ) + geom_boxplot() + xlab("Race") + ylab("Age (in years) at Marriage") #or: ggplot( data = Marriage, aes( x = officialTitle, y = age ) ) + geom_boxplot() + xlab("Type of Officiant") + ylab("Age (in years) at Marriage") + coord_flip() # (c) # Simple scatterplot using geom_point g3 <- ggplot(data = Marriage, aes(y = age, x = dob)) # Addition of information on other variables using the Size, Color aesthetics and facets: g3 + geom_point(data=Marriage, aes(color = officialTitle), size = Marriage$prevcount+1) + facet_wrap(~ race) #Problem 7, Chapter 3 Exercises: ## Creating a basic ggplot object g7 <- ggplot( data = RailTrail, aes(x = hightemp, y = volume) ) + geom_point() g7 ## Plotting 2 separate scatterplots for the two weekday categories g7 + facet_wrap(~ weekday) ## Plotting it as a scatterplot with a trend line: g7 <- g7 + geom_smooth(method = "lm", se = FALSE) + # Note "lm" will show a LINEAR trend xlab("High Temperature") + ylab("Number of Crossings") ## Plotting 2 separate scatterplots for the two weekday categories (now with trend lines) g7 + facet_wrap(~ weekday)