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
ggplot(data = Marriage, aes(y = fct_rev(fct_infreq(race)), fill = race)) +
geom_bar() +
theme(legend.position = 'none') +
labs(x = "Count", y = "Race",
title = "Number of Marriages of different Race")
Lenth of the bar and colors are the visual cues. The number of marriages(count) and race are the 2 variables. Lenght of the bar is number of marriages and the color depicts different race.
ggplot(data = Marriage, aes(x = prevconc, y = delay)) +
geom_point(aes(color = race, shape = person)) +
facet_wrap(~officialTitle)
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.
ggplot(mpg, aes(x = hwy, y = manufacturer)) +
geom_boxplot() +
theme_classic() +
labs(x = "Highway Fuel Efficiency (miles/gallon)", y = "Vehicle Manufacturer")
This graphic is built with the diamonds dataset in the
ggplot2 package.
ggplot(diamonds, aes(x = price, fill = cut, color = cut)) +
geom_density(alpha = 0.2, size = 0.2) +
scale_fill_discrete() +
labs(fill = 'Cut', x = "Diamond Price (USD)", y = "Density", title = "Diamond Price Density") +
guides(color = FALSE)
This graphic uses the penguins dataset and shows the
counts between males and females by species.
ggplot(penguins, aes(y = sex, fill = species)) +
geom_bar(alpha = 0.8) +
scale_fill_brewer(palette = 'Accent') +
facet_wrap(~ species, ncol = 1) +
theme_minimal() +
theme(legend.position = 'none') +
labs(x = "Count", y = "Sex")
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")