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 hyperlinked and that I can see the
visualization and the code required to create it
(echo=TRUE).
mosaicData package.My plot uses the visual cue of length to represent the average Age of marriage separated by Bride and Groom. On average, the Grooms are 3 years older than the Bride.
The coordinate system is Cartesian. Context is provided by the title and axis labels.
Marriage %>%
group_by(person) %>%
summarize(avg_age = round(mean(age), 0)) %>%
ggplot(aes(x = person, y = avg_age)) +
theme_classic() +
geom_bar(stat = "identity") +
geom_text(aes(label = avg_age), vjust = -0.3, size = 4) +
scale_y_continuous(expand = c(0,0),
limits = c(0,38)) +
# Change the appearance of axis tick labels
theme(axis.text.x = element_text(face="bold", size=14)) +
labs(x = NULL, y = "Average Marriage Age",
title = "What Is the Average Age of Marriage in Mobile County, Alabama?")
ggplot(data = Marriage, aes(x=age, y=college, color = person, shape = race, size = hs)) +
geom_point() +
theme_classic() +
labs(x = "Age", y = "Years in College",
title = "The Relationship between Age of Marriage and Years in College by Race, Title, and Years in High School")
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(manufacturer, hwy)) +
geom_boxplot() +
coord_flip() +
labs(x="Vehicle Manufacturer", y="Highway Fuel Efficiency (miles/gallon)") +
theme_classic()
This graphic is built with the diamonds dataset in the
ggplot2 package.
ggplot(diamonds, aes(price,fill=cut,color=cut)) +
geom_density(alpha=0.3, size=0.3) +
scale_fill_discrete_qualitative(palette = "Dark 3") +
labs(x="Diamond Price (USD)", y="Density", title="Diamond Price Density")
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_manual(values = c("darkorange","purple","cyan4"),
guide = FALSE) + #makes legend go away
theme_minimal() +
facet_wrap(~species, ncol = 1) +
coord_flip() +
labs(y="Count", x="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)) +
geom_point(aes(color = species,
shape = species),
size = 2) +
guides(shape = 'none') + #remove extra legends
geom_smooth(method = "lm", se = FALSE, aes(color = species)) +
scale_color_manual(values = c("darkorange","darkorchid","cyan4")) +
labs(
color = 'Species', #legend label
x = 'Bill Length (mm)',
y = 'Bill Depth (mm)')