Directions

The objective of this assignment is to complete and explain basic plots before moving on to more complicated ways to graph data.

Each question is worth 5 points.

To submit this homework you will create the document in Rstudio, using the knitr package (button included in Rstudio) and then submit the document to your Rpubs account. Once uploaded you will submit the link to that document on Canvas. Please make sure that this link is hyper linked and that I can see the visualization and the code required to create it (echo=TRUE).

Questions

  1. For The following questions use the Marriage data set from the mosaicData package.
ggplot(data = Marriage, aes(y = fct_rev(fct_infreq(race)), fill = race)) + 
  geom_bar() +
  theme(legend.position = 'none') +
  labs(x = "Count", y = "Race")

ggplot(data = Marriage, aes(x=age, y=college, color = person, shape = race, size = hs)) + 
  geom_point() +
  theme_bw() +
  labs(x = "Age", y = "Years in College",
       title = "Marriage Age vs Years in College by Offical Title, Race and Years in High School")
## Warning: Removed 10 rows containing missing values (geom_point).

Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.

  1. Boxplot Visualization

This boxplot was built using the mpg dataset. Notice the changes in axis labels.

boxplot_viz <- ggplot(mpg, aes(manufacturer, hwy)) 
boxplot_viz + geom_boxplot() + coord_flip() + labs(y = "Highway Fuel Efficiency (miles/gallon)", x = "Vehicle Manufacturer") + theme_classic()

  1. Stacked Density Plot

This graphic is built with the diamonds dataset in the ggplot2 package.

stacked_density_plot <- ggplot(diamonds, aes(x = price, fill = cut, color = cut)) 
stacked_density_plot + geom_density(alpha = 1/5) + labs(x = "Diamond Price (USD)", y = "Density", title = "Diamond Price Density")  

  1. Sideways bar plot

This graphic uses the penguins dataset and shows the counts between males and females by species.

ggplot(penguins, aes(y = sex, fill = species)) + 
  geom_bar() +
  scale_fill_manual(values = c("darkorange", "purple", "cyan4")) + 
  facet_wrap(~ species, ncol = 1) + 
  theme_minimal() + 
  theme(legend.position = 'none') + 
  labs(x = "Count", y = "Sex")

  1. Scatterplot

This figure examines the relationship between bill length and depth in the penguins dataset.

ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point(aes(shape = species), size = 2) +
  geom_smooth(formula = 'y ~ x', method = 'lm', se = FALSE) +
  scale_color_manual(values = c("darkorange", "darkorchid", "cyan4")) +
  labs(color = 'Species', x = 'Bill Length (mm)', y = 'Bill Depth (mm)') + 
  guides(shape = FALSE)