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
# Create an informative and meaningful data graphic.
ggplot(marriage, aes(x = college, fill = race)) +
geom_histogram(binwidth = 1, position = "dodge")
## Warning: Removed 10 rows containing non-finite values (`stat_bin()`).
summary(marriage$race)
## American Indian Black Hispanic White
## 1 22 1 74
# Create a data graphic with at least five variables (either quantitative or categorical).
ggplot(data = marriage, aes(x = age, y = college)) +
geom_point(aes(color = sign, shape = race), size = 2) +
facet_wrap(~person)
## 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.
This boxplot was built using the mpg dataset. Notice the
changes in axis labels.
# load package
library(ggplot2)
p2 <- ggplot(mpg,aes(manufacturer,hwy))
# draw boxplot & change axis lines to vertical
p2 + 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.
# check if ggthemes installed, if the required library is not found then install it
if (!require("ggthemes")) {
install.packages("ggthemes")
}
## Loading required package: ggthemes
# load package
library(ggthemes)
library(ggplot2)
p3 <- ggplot(diamonds, aes(x=price,color=cut,fill=cut))
# draw density plot
p3 <- p3 + geom_density(alpha=0.2) + labs(x="Diamond Price(USD)", y="Density", title="Diamond Price Density")
# Economist background theme and color scales
p3 + theme_economist() + scale_colour_economist()
This graphic uses the penguins dataset and shows the
counts between males and females by species.
# load package
library(ggplot2)
p4 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length))
# draw scatter plot
p4 <- p4 + geom_jitter() + labs(x="Iris Sepal Length", y="Iris Petal Length", title="Relationship between Petal and Sepal Length")
# fit a linear model to the data
p4 <- p4 + geom_smooth(method=lm)
# change theme
p4 + theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
This figure examines the relationship between bill length and depth
in the penguins dataset.
# load package
library(ggplot2)
p5 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, color = Species))
# draw scatter plot
p5 <- p5 + geom_jitter() + labs(x="Iris Sepal Length", y="Iris Petal Length", title="Relationship between Petal and Sepal Length", subtitle="Species level comparison")
# fit a linear model to the data
p5 <- p5 + geom_smooth(se = FALSE, method=lm)
# change theme
p5 <- p5 + theme_tufte()
# change legend position
p5 + theme(legend.position = "bottom")