ggplot2The 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).
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
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))+
geom_point(aes(color=race))+
scale_colour_manual(values=c("blue","pink","orange","green"))+
labs(color="race", title = "Years in College and 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", y= "Official Title", title="Official Title and Marriage Delays")
Your objective for the next four questions will be write the code necessary to exactly recreate the provided graphics.
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 efficiency", y= "vehicle manufacturer")
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(viridisLite)
library(viridis)
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", y="Density")
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(alpha=0.8)+
scale_fill_manual(values=c("pink","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.
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("pink","orange","purple"))+
labs(color="Species",x="Bill Length",y="Bill Depth")