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)
##   bookpageID    appdate ceremonydate delay     officialTitle person        dob
## 1   B230p539 1996-10-29   1996-11-09    11    CIRCUIT JUDGE   Groom 1964-04-11
## 2   B230p677 1996-11-12   1996-11-12     0 MARRIAGE OFFICIAL  Groom 1964-08-06
## 3   B230p766 1996-11-19   1996-11-27     8 MARRIAGE OFFICIAL  Groom 1962-02-20
## 4   B230p892 1996-12-02   1996-12-07     5          MINISTER  Groom 1956-05-20
## 5   B230p994 1996-12-09   1996-12-14     5          MINISTER  Groom 1966-12-14
## 6  B230p1209 1996-12-26   1996-12-26     0 MARRIAGE OFFICIAL  Groom 1970-02-21
##        age     race prevcount prevconc hs college dayOfBirth        sign
## 1 32.60274    White         0     <NA> 12       7        102       Aries
## 2 32.29041    White         1  Divorce 12       0        219         Leo
## 3 34.79178 Hispanic         1  Divorce 12       3         51      Pisces
## 4 40.57808    Black         1  Divorce 12       4        141      Gemini
## 5 30.02192    White         0     <NA> 12       0        348 Saggitarius
## 6 26.86301    White         1     <NA> 12       0         52      Pisces
ggplot(Marriage, aes(x=college, y=age, col=sign)) +
  geom_jitter() +
  labs(y="Age", x="Number of Years in College", title="Age at Marriage based on College Education")
## Warning: Removed 10 rows containing missing values (geom_point).

In the graphic above, we are examining the relationship between age at marriage based on college education and zodiac sign. As seen in the graph I used color cues to graph zodiac sign into the graphic, I placed age at time of marriage in the y-axis and I placed the number of years in college on the x-axis. As can be seen additionally there does not seem to be a strong correlation between age at time of marriage and years in college, and neither with the zodiac sign.

The coordinate system in the above graphic is Cartesian, as we are using both the x and the y axis to plot the data.

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(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.

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
ggplot(diamonds, aes(price,fill=cut,color=cut)) + 
  geom_density(alpha=0.5, size=0.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.

head(penguins)
## # A tibble: 6 × 8
##   species island    bill_length_mm bill_depth_mm flipper_l…¹ body_…² sex    year
##   <fct>   <fct>              <dbl>         <dbl>       <int>   <int> <fct> <int>
## 1 Adelie  Torgersen           39.1          18.7         181    3750 male   2007
## 2 Adelie  Torgersen           39.5          17.4         186    3800 fema…  2007
## 3 Adelie  Torgersen           40.3          18           195    3250 fema…  2007
## 4 Adelie  Torgersen           NA            NA            NA      NA <NA>   2007
## 5 Adelie  Torgersen           36.7          19.3         193    3450 fema…  2007
## 6 Adelie  Torgersen           39.3          20.6         190    3650 male   2007
## # … with abbreviated variable names ¹​flipper_length_mm, ²​body_mass_g
ggplot(penguins, aes(x = sex, fill = species)) +
  geom_bar() +
  facet_wrap(~species, ncol = 1) +
  scale_fill_manual(values = c("darkorange","purple","darkcyan"), guide = FALSE) +
  theme_minimal() +
  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.

head(penguins)
## # A tibble: 6 × 8
##   species island    bill_length_mm bill_depth_mm flipper_l…¹ body_…² sex    year
##   <fct>   <fct>              <dbl>         <dbl>       <int>   <int> <fct> <int>
## 1 Adelie  Torgersen           39.1          18.7         181    3750 male   2007
## 2 Adelie  Torgersen           39.5          17.4         186    3800 fema…  2007
## 3 Adelie  Torgersen           40.3          18           195    3250 fema…  2007
## 4 Adelie  Torgersen           NA            NA            NA      NA <NA>   2007
## 5 Adelie  Torgersen           36.7          19.3         193    3450 fema…  2007
## 6 Adelie  Torgersen           39.3          20.6         190    3650 male   2007
## # … with abbreviated variable names ¹​flipper_length_mm, ²​body_mass_g
ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
  geom_point(aes(shape = species), size = 2) +
  scale_color_manual(values = c("darkorange", "darkorchid", "darkcyan")) +
  geom_smooth(formula = 'y ~ x', method = 'lm', se = FALSE) +
  labs( x = 'Bill Length (mm)', y = 'Bill Depth (mm)', color = 'Species')