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.marriage <- Marriage
ggplot(marriage, aes(x = prevcount, fill = race)) +
geom_histogram(binwidth = 1, position = "dodge")
- Create a data graphic with at least five variables (either
quantitative or categorical). For the purposes of this exercise, do not
worry about making your visualization meaningful—just try to encode five
variables into one plot. Your objective for the next four questions will
be write the code necessary to exactly recreate the
provided graphics.
marriage <- Marriage
ggplot(data = marriage, aes(x = age, y = college)) +
geom_point(aes(color = college, shape = race), size = 2) +
facet_wrap(~person)
## Warning: Removed 10 rows containing missing values (`geom_point()`).
This boxplot was built using the mpg dataset. Notice the
changes in axis labels.
ggplot(mpg,aes(manufacturer,hwy))+geom_boxplot()+coord_flip()+theme_classic()+labs(x="Vehicle Manufacturer",y = "Highway Fuel Efficiency (mile/gallon)")
This graphic is built with the diamonds dataset in the
ggplot2 package.
library(ggthemes)
ggplot(diamonds,aes(x=price,color=cut,fill=cut))+geom_density(aes(fill=factor(cut)),alpha=0.3)+labs(title = "Diamond Price Density", x="Diamond Price (USD)", y="Density")+theme_economist()
This graphic uses the penguins dataset and shows the
counts between males and females by species.
penguins <- penguins
penguins %>%
count(sex, species) %>%
ggplot() +
geom_col(aes(x = sex, y = n, fill = species)) +
scale_fill_manual(values = c("darkorange", "purple", "cyan4")) +
facet_wrap(~species, ncol = 1) +
theme_minimal() +
theme(legend.position = 'none') +
labs(x = "Sex", y = "Count") +
coord_flip()
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")