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.
mpg.plot <- ggplot(mpg) # Create plot for mpg dataset
mpg.plot + # Use stacked bar chart. x-asix is class,
geom_bar(aes(class, fill = trans)) +
scale_fill_discrete(name = "transmission") # Rename legend title
mpg.plot + # Create plot for mpg dataset
geom_boxplot(aes(manufacturer, hwy)) +
theme_classic() + # Use the classic theme
coord_flip() + # Flip coordinate
labs(y = "Highway Fuel Efficiency (mile/gallon)", x = "Vehicle Manufacturer")
ggplot(diamonds) + # Create plot for diamonds dataset
geom_density(aes(price, # Find density of diamond price
fill = cut,color = cut),alpha = 0.3,size = 0.6) +
labs(title = "Diamond Price Density",x = "Diamond Price (USD$)",y = "Density") +theme_economist()# Use theme similar to The Economist
ggplot(iris, # Create plot for iris dataset
aes(Sepal.Length, Petal.Length)) +
geom_point() + # Use scatter plot
geom_smooth(method = lm) + # Add a regression line
theme_minimal() + # Use the "minimal" theme
theme(panel.grid.major = element_line(size = 1), # Set width of major grid line
panel.grid.minor = element_line(size = 0.7)) + # Set width of minor grid line
labs(title = "Relationship between Petal and Sepal Length",
x = "Iris Sepal Length",
y = "Iris Petal Length")
ggplot(iris, # Create plot for iris dataset
aes(Sepal.Length,
Petal.Length,
color = Species)) + # Use "Species" as the type of legend, and use colors to differentiate
geom_point() + # Use scatter plot
geom_smooth(method = lm, se = FALSE) + # Draw regression line without confidence region
theme_pander() +
theme(text = element_text(family = "serif"), # Use fond: Times news roman
axis.ticks = element_line(color = "black",
size = 0.7),
legend.position = "bottom", # Move legend to the bottom of plot
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")