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.
#meaningful data graphic
data = mosaicData::Marriage
table(data$race)
## 
## American Indian           Black        Hispanic           White 
##               1              22               1              74
ggplot(data, aes(x = person, y = age))+
  geom_boxplot()

#This simple box plot shows that, on average, grooms are older than brides. By looking at the plot we can see that the avg age for males is around 35, while the avg age for females is around 28. We can also see that the distribution of age is smaller for grooms than brides (i.e less volatile)

#5 variable data graphic
ggplot(data, aes(x = sign, y = age, fill = prevconc))+
  geom_boxplot(aes(colour = person, shape = race))

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.

data2 = mpg

ggplot(data = mpg,aes(x = hwy, y = manufacturer))+
  geom_boxplot()+
  labs(x="Highway Fuel Efficienciy (miles/gallon)")+
  labs(y="Vehicle Manufacturer")

  1. Stacked Density Plot

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

data3 = diamonds

ggplot(data=diamonds, aes(x=price, group=cut, fill=cut))+
    geom_density(adjust=1.5, alpha = .2)+
    labs(x="Diamond Price (USD)")+
    labs(y="Density")+
    ggtitle("Diamond Price Density")

  1. Sideways bar plot

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

data4 = penguins

data4%>%
  count(sex, species) %>%
    ggplot()+
      geom_col(aes(x = sex, y = n, fill = species)) +
      scale_fill_manual(values = c("orange", "purple", "green")) + 
      facet_wrap(~species, ncol = 1) + 
      theme_minimal() + 
      theme(legend.position = 'none') + 
      labs(x = "Sex", y = "Count") + 
      coord_flip()

  1. Scatterplot

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

ggplot(data = penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species))+ 
  geom_point(aes(shape = species)) + 
  geom_smooth(method = 'lm', se = FALSE) +
  scale_color_manual(values = c("orange", "purple", "green")) + 
  labs(x = "Bill Length (mm)", y = "Bill Depth (mm)")