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.
mdata=mosaicData::Marriage
head(mdata)
##   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
summary(mdata)
##      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
str(mdata)
## 'data.frame':    98 obs. of  15 variables:
##  $ bookpageID   : Factor w/ 49 levels "B230p1209","B230p1354",..: 5 6 7 8 9 1 2 3 4 16 ...
##  $ appdate      : Date, format: "1996-10-29" "1996-11-12" ...
##  $ ceremonydate : Date, format: "1996-11-09" "1996-11-12" ...
##  $ delay        : int  11 0 8 5 5 0 16 0 28 10 ...
##  $ officialTitle: Factor w/ 9 levels "BISHOP","CATHOLIC PRIEST",..: 4 6 6 7 7 6 6 6 6 7 ...
##  $ person       : Factor w/ 2 levels "Bride","Groom": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dob          : Date, format: "1964-04-11" "1964-08-06" ...
##  $ age          : num  32.6 32.3 34.8 40.6 30 ...
##  $ race         : Factor w/ 4 levels "American Indian",..: 4 4 3 2 4 4 4 4 2 4 ...
##  $ prevcount    : int  0 1 1 1 0 1 1 1 0 3 ...
##  $ prevconc     : Factor w/ 2 levels "Death","Divorce": NA 2 2 2 NA NA 2 2 NA 2 ...
##  $ hs           : int  12 12 12 12 12 12 12 12 12 12 ...
##  $ college      : int  7 0 3 4 0 0 0 0 0 6 ...
##  $ dayOfBirth   : num  102 219 51 141 348 52 284 31 338 183 ...
##  $ sign         : Factor w/ 12 levels "Aquarius","Aries",..: 2 6 8 5 9 8 7 1 9 3 ...
ggplot(mdata, aes(x=age,fill=person))+
  geom_histogram(position = "dodge")+
  labs(title="Married People by Age and Person", x="Age",y="Count",fill="Person")+
  scale_x_continuous(breaks=seq(15,75,5))+
  theme_bw()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

library(cowplot)
## Warning: package 'cowplot' was built under R version 4.2.2
plt=ggplot(mdata, aes(x=hs, y=age, color=sign, shape=ifelse(is.na(prevconc),"No Data", ifelse(prevconc=="Death","Death","Divorce")), size=college))+
  geom_point()+
  labs(title="Demographic Representation of the Married", x="Classes in High School",y="Age",color="Zodiac Sign",shape="Why Last Marriage Ended",size="Years of College")+
  facet_wrap(~person)

guide_col=get_legend(plt+
            guides(shape="none",size="none"))
## Warning: Removed 10 rows containing missing values (geom_point).
plot_grid(plt+
  guides(color="none")+ 
  theme(legend.position="bottom"),guide_col,rel_widths=c(0.95, 0.15))
## 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.

mpgdata = mpg
head(mpgdata)
## # 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…
summary(mpgdata)
##  manufacturer          model               displ            year     
##  Length:234         Length:234         Min.   :1.600   Min.   :1999  
##  Class :character   Class :character   1st Qu.:2.400   1st Qu.:1999  
##  Mode  :character   Mode  :character   Median :3.300   Median :2004  
##                                        Mean   :3.472   Mean   :2004  
##                                        3rd Qu.:4.600   3rd Qu.:2008  
##                                        Max.   :7.000   Max.   :2008  
##       cyl           trans               drv                 cty       
##  Min.   :4.000   Length:234         Length:234         Min.   : 9.00  
##  1st Qu.:4.000   Class :character   Class :character   1st Qu.:14.00  
##  Median :6.000   Mode  :character   Mode  :character   Median :17.00  
##  Mean   :5.889                                         Mean   :16.86  
##  3rd Qu.:8.000                                         3rd Qu.:19.00  
##  Max.   :8.000                                         Max.   :35.00  
##       hwy             fl               class          
##  Min.   :12.00   Length:234         Length:234        
##  1st Qu.:18.00   Class :character   Class :character  
##  Median :24.00   Mode  :character   Mode  :character  
##  Mean   :23.44                                        
##  3rd Qu.:27.00                                        
##  Max.   :44.00
ggplot(mpgdata,aes(x=manufacturer,y=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.

ddata = diamonds
head(ddata)
## # 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
summary(ddata)
##      carat               cut        color        clarity          depth      
##  Min.   :0.2000   Fair     : 1610   D: 6775   SI1    :13065   Min.   :43.00  
##  1st Qu.:0.4000   Good     : 4906   E: 9797   VS2    :12258   1st Qu.:61.00  
##  Median :0.7000   Very Good:12082   F: 9542   SI2    : 9194   Median :61.80  
##  Mean   :0.7979   Premium  :13791   G:11292   VS1    : 8171   Mean   :61.75  
##  3rd Qu.:1.0400   Ideal    :21551   H: 8304   VVS2   : 5066   3rd Qu.:62.50  
##  Max.   :5.0100                     I: 5422   VVS1   : 3655   Max.   :79.00  
##                                     J: 2808   (Other): 2531                  
##      table           price             x                y         
##  Min.   :43.00   Min.   :  326   Min.   : 0.000   Min.   : 0.000  
##  1st Qu.:56.00   1st Qu.:  950   1st Qu.: 4.710   1st Qu.: 4.720  
##  Median :57.00   Median : 2401   Median : 5.700   Median : 5.710  
##  Mean   :57.46   Mean   : 3933   Mean   : 5.731   Mean   : 5.735  
##  3rd Qu.:59.00   3rd Qu.: 5324   3rd Qu.: 6.540   3rd Qu.: 6.540  
##  Max.   :95.00   Max.   :18823   Max.   :10.740   Max.   :58.900  
##                                                                   
##        z         
##  Min.   : 0.000  
##  1st Qu.: 2.910  
##  Median : 3.530  
##  Mean   : 3.539  
##  3rd Qu.: 4.040  
##  Max.   :31.800  
## 
ggplot(ddata,aes(x=price,color=cut,fill=cut))+
  geom_density(alpha=0.25)+
  labs(title="Diamond Price Density", x="Diamond Price (USD)", y="Density",guides="Cut")+
  scale_fill_discrete()+
  guides(fill = guide_legend(override.aes = list(color = "black")))

  1. Sideways bar plot

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

pdata = penguins
head(pdata)
## # 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
summary(pdata)
##       species          island    bill_length_mm  bill_depth_mm  
##  Adelie   :152   Biscoe   :168   Min.   :32.10   Min.   :13.10  
##  Chinstrap: 68   Dream    :124   1st Qu.:39.23   1st Qu.:15.60  
##  Gentoo   :124   Torgersen: 52   Median :44.45   Median :17.30  
##                                  Mean   :43.92   Mean   :17.15  
##                                  3rd Qu.:48.50   3rd Qu.:18.70  
##                                  Max.   :59.60   Max.   :21.50  
##                                  NA's   :2       NA's   :2      
##  flipper_length_mm  body_mass_g       sex           year     
##  Min.   :172.0     Min.   :2700   female:165   Min.   :2007  
##  1st Qu.:190.0     1st Qu.:3550   male  :168   1st Qu.:2007  
##  Median :197.0     Median :4050   NA's  : 11   Median :2008  
##  Mean   :200.9     Mean   :4202                Mean   :2008  
##  3rd Qu.:213.0     3rd Qu.:4750                3rd Qu.:2009  
##  Max.   :231.0     Max.   :6300                Max.   :2009  
##  NA's   :2         NA's   :2
ggplot(pdata, aes(x=sex,fill=species))+
  geom_bar(alpha=0.75)+
  scale_fill_manual(values = c("darkorange","purple","cyan4"), guide = "none")+
  coord_flip()+
  labs(x="Sex",y="Count")+
  facet_wrap(~species,ncol=1)+ 
  theme_minimal()

  1. Scatterplot

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

ggplot(pdata, aes(x=bill_length_mm,y=bill_depth_mm))+
  geom_point(aes(color=species, shape=species), size=2)+ 
  geom_smooth(method="lm", se=FALSE, aes(color=species))+
  scale_color_manual(values=c("darkorange","darkorchid","cyan4"))+
  labs(x="Bill Length (mm)", y="Bill Depth (mm)",color="Species")+
  guides(shape="none")