The objectives of this problem set is to gain experience working with the ggplot2 package for data visualization. To do this I have provided a series of graphics, all created using the ggplot2 package. Your objective for this assignment will be write the code necessary to exactly recreate the provided graphics.
When completed submit a link to your file on rpubs.com. Be sure to include echo = TRUE for each graphic so that I can see the visualization and the code required to create it.
## Load packages to be used in this exercise
require(datasets)
require(ggplot2)
## Loading required package: ggplot2
require(ggthemes)
## Loading required package: ggthemes
## bar chart based on mpg dataset
## X-asix is class, y is actual counts
## Legend is transmission
mpg<- ggplot(mpg)
mpg +
geom_bar(aes(class, fill = trans)) +
scale_fill_discrete(name = "Transmission")
## box plot for distribution of MPG by Manufacturer
## Use the classic theme
## Flip coordinate
## Assign y-axis and x-axis title
mpg +
geom_boxplot(aes(manufacturer, hwy)) +
theme_classic() +
coord_flip() +
labs(y = "Highway Fuel Efficiency (mile/gallon)",
x = "Vehicle Manufacturer")
## based is plot for diamonds dataset
## density function for diamond price
## Legend is cut.
## color by transparency level
library(ggthemes)
ggplot(diamonds) +
geom_density(aes(price, fill = cut, color = cut), alpha = 0.3, size = 0.6) +
labs(title = "Diamond Price Density", x = "Diamond Price (USD)", y = "Density") +
theme_economist()
## Switch to iris dataset
## X-axis is sepal length, y-axis is patel length
## "minimal" themeine
ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point() +
geom_smooth(method = lm) +
theme_minimal() +
theme(panel.grid.major = element_line(size = 1),
panel.grid.minor = element_line(size = 0.7)) +
labs(title = "Relationship between Petal and Sepal Length", x = "Iris Sepal Length", y = "Iris Petal Length")
## keep x and y axis as last example
## color by species
## scatter plot by geom_point
## Use the Pander theme
## Move legend to the bottom
ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) +
geom_point() +
geom_smooth(method = lm, se = FALSE) +
theme_pander() +
theme(axis.ticks = element_line(color = "black", size = 0.7),
legend.position = "bottom",
legend.title = element_text(face = "plain"),
plot.title = element_text(size = 14, face = "plain")) +
labs(title = "Relationship between Petal and Sepal Length",
subtitle = "Species level comparison", x = "Iris Sepal Length", y = "Iris Petal Length")