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.
summary(Marriage)
##      bookpageID    appdate            ceremonydate            delay       
##  B230p1209: 2   Min.   :1996-10-29   Min.   :1996-11-09   Min.   : 0.000  
##  B230p1354: 2   1st Qu.:1997-04-22   1st Qu.:1997-04-26   1st Qu.: 0.000  
##  B230p1665: 2   Median :1997-11-26   Median :1997-11-26   Median : 3.000  
##  B230p1948: 2   Mean   :1997-12-04   Mean   :1997-12-10   Mean   : 5.673  
##  B230p539 : 2   3rd Qu.:1998-07-13   3rd Qu.:1998-07-31   3rd Qu.: 9.000  
##  B230p677 : 2   Max.   :1999-02-05   Max.   :1999-02-06   Max.   :28.000  
##  (Other)  :86                                                             
##            officialTitle   person        dob                  age       
##  MARRIAGE OFFICIAL:44    Bride:49   Min.   :1924-05-21   Min.   :16.27  
##  PASTOR           :22    Groom:49   1st Qu.:1955-03-02   1st Qu.:21.66  
##  MINISTER         :20               Median :1965-05-04   Median :31.90  
##  BISHOP           : 2               Mean   :1963-06-15   Mean   :34.51  
##  CATHOLIC PRIEST  : 2               3rd Qu.:1976-02-07   3rd Qu.:42.82  
##  CHIEF CLERK      : 2               Max.   :1982-07-20   Max.   :74.25  
##  (Other)          : 6                                                   
##               race      prevcount         prevconc        hs       
##  American Indian: 1   Min.   :0.0000   Death  : 7   Min.   : 8.00  
##  Black          :22   1st Qu.:0.0000   Divorce:43   1st Qu.:12.00  
##  Hispanic       : 1   Median :1.0000   NA's   :48   Median :12.00  
##  White          :74   Mean   :0.7755                Mean   :11.68  
##                       3rd Qu.:1.0000                3rd Qu.:12.00  
##                       Max.   :5.0000                Max.   :12.00  
##                                                                    
##     college        dayOfBirth              sign   
##  Min.   :0.000   Min.   :  6.00   Pisces     :16  
##  1st Qu.:0.000   1st Qu.: 81.25   Aries      :10  
##  Median :1.000   Median :166.50   Virgo      :10  
##  Mean   :1.625   Mean   :177.76   Gemini     : 9  
##  3rd Qu.:2.000   3rd Qu.:263.75   Saggitarius: 9  
##  Max.   :7.000   Max.   :358.00   Cancer     : 8  
##  NA's   :10                       (Other)    :36
ggplot(Marriage, aes(x = college, fill = race)) + 
  geom_histogram(binwidth = 1, position = "dodge")
## Warning: Removed 10 rows containing non-finite values (`stat_bin()`).

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, colour = 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(x = sex, fill = species)) +
  geom_bar(alpha = 0.8) +
  scale_fill_brewer(palette = 'Dark2') +
  theme_minimal() +
  facet_wrap(~species, ncol = 1) +
  coord_flip() +
  labs(x ="Sex", y = "Count")

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