ggplot2
The 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.df<-Marriage
plot1<-ggplot(df, aes(x = college, fill = race)) +
geom_histogram(binwidth = 1.5, position = "dodge")
plot1
## Warning: Removed 10 rows containing non-finite values (`stat_bin()`).
plot2<-ggplot(df, aes(x = race, y = officialTitle, color = race)) +
geom_point(aes(color = sign, shape = race), size = 2) +
labs(title = 'Does race impact officialTitle?')+
facet_wrap(~person)
plot2
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(hwy, manufacturer)) +
geom_boxplot() +
labs(x="Highway Fuel Efficiency (miles/gallon)", y="Vehicle Manufacturer") +
theme_classic()
This graphic is built with the diamonds
dataset in the
ggplot2
package.
diamonds
## # A tibble: 53,940 × 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
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4 4.05 2.39
## # … with 53,930 more rows
ggplot(diamonds, aes(price,fill=cut,color=cut)) +
geom_density(alpha=0.3, size=0.3) +
theme_bw() +
scale_fill_discrete() +
labs(x="Diamond Price (USD)", y="Density", title="Diamond Price Density")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
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 = "none") +
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(penguins, aes(bill_length_mm,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("darkorange","darkorchid","cyan4")) +
labs(color = 'Species', x = 'Bill Length (mm)', y = 'Bill Depth (mm)')