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.
Marriage %>% 
  group_by(race) %>% 
  summarize(age_avg = round(mean(age),0))%>%
  mutate(race = fct_reorder(race, age_avg)) %>%
  ggplot(aes(race,age_avg))+
  geom_bar(stat = "identity",aes(fill=race)) +
  coord_flip()+
  geom_text(aes(label=age_avg), hjust=-0.1, size=4)+
  labs(title = ("Avb Marriage Age By Race"),
       x = "Race",
       y = "Avg Age")+
  theme(axis.text=element_text(size=11),
        axis.title=element_text(size=12,face="bold"))

ggplot(Marriage,aes(age, delay, color = prevconc, shape = race)) +
    geom_point() +
    facet_wrap(~ person, nrow = 1) +
    labs(title = ("Relationship Between Delay and Age by Person, Prevconc, and Race"),
         x = "Age",
         y = "Delay")

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.

library(ggplot2)

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

  1. Stacked Density Plot

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

ggplot(diamonds, aes(price, fill = cut, color = cut)) + 
  geom_density(alpha = 0.3, size = 0.3) +
  scale_fill_brewer(palette = "Paired")+
  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(x = sex, fill = species)) + geom_bar(alpha = 0.8) +
  
  scale_fill_manual(values = c("darkorange","purple","cyan4"), guide = FALSE) + 
  
  theme_minimal() + facet_wrap(~species, ncol = 1) + coord_flip() + labs(y="Count", x="Sex")
## Warning: The `guide` argument in `scale_*()` cannot be `FALSE`. This was deprecated in
## ggplot2 3.3.4.
## i Please use "none" instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

  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)) + geom_point(aes(color = species, shape = species), size = 2)  +
  
  guides(shape = 'none') + geom_smooth(method = "lm", se = FALSE, aes(color = species)) +
  
    scale_color_manual(values = c("darkorange","darkorchid","cyan4"))  + labs(color = 'Species', x = 'Bill Length (mm)', y = 'Bill Depth (mm)')