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.
#head(Marriage)
ggplot(Marriage, aes(x = college, y = age)) +
  geom_point(aes(color = race)) +
  scale_colour_manual(values = c("blue","pink","red","purple")) +
  labs(color = "race", title = "Years in College vs Age",x = "Years in College", y = "Age in Years")
## Warning: Removed 10 rows containing missing values (geom_point).

ggplot(Marriage, aes(x=delay, y=officialTitle)) +
  geom_point(aes(color=sign,shape=race)) + scale_color_discrete()+
  labs(color = "sign", x="Delay (in days)", y="Official Title", title = "Official Title vs Marriage Delays")

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.

head(mpg)
## # A tibble: 6 × 11
##   manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
##   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr> 
## 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa…
## 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa…
## 3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa…
## 4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa…
## 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa…
## 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa…
ggplot(mpg, aes(x=hwy, y=manufacturer))+
  geom_boxplot() +
  theme_classic()+
  labs(x="Highway Fuel Efficiency (miles/gallon", y="Vehicle Manufacturer")

  1. Stacked Density Plot

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

head(diamonds)
## # A tibble: 6 × 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
library(viridis)
## Loading required package: viridisLite
ggplot(diamonds, aes(x=price, color=cut, fill=cut)) +
  geom_density(alpha=.2)+
  scale_fill_discrete()+
  scale_color_viridis(discrete=TRUE)+
  labs(fill = "Cut",color="Cut", x="Diamond Price (USD)", y="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: It is deprecated to specify `guide = FALSE` to remove a guide. Please
## use `guide = "none"` instead.

  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)")