This graphic is a traditional stacked bar chart. This graphic works on the mpg dataset, which is built into the ggplot2 library. Use scale_fill_discrete to rename the legend trans.
library(ggplot2)
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = trans)) +
scale_fill_discrete(name = "Transmission") # rename legend trans to be Transmission for more clarity
This boxplot is also built using the mpg dataset. Use coord_flip to flip x/y axis. Alter theme use theme_classic()
h <- ggplot(mpg, aes(manufacturer, hwy))
h + geom_boxplot() +
coord_flip() +
labs(x = "Vehicle Manufacturer", y = "Highway Fuel Efficiency (miles/gallon)") +
theme_classic()
This graphic is built with another dataset diamonds a dataset also built into the ggplot2 package. Addtional package library(ggthemes) called.
library(ggthemes)
ggplot(diamonds, aes(price, fill = cut)) +
geom_density(alpha = 0.2 ) +
scale_fill_manual(values = c("red", "gold", "light green", "dark blue", "hot pink")) +
labs(x = "Diamon Price (USD)", y = "Density", title = "Diamond Price Density") +
theme_economist() + scale_colour_economist()
For this plot we are changing vis idioms to a scatter plot framework. ggplot2 package is used to fit a linear model. There are edited labels and theme modifications as well.
ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point() +
geom_smooth(method = "lm") +
labs(x = "Iris Sepal Length", y = "Iris Petal Length", title = "Relationship between Petal and Sepal Length") +
theme_bw() + theme(panel.border = element_blank())
Finally, in this vis I extend on the last example, by plotting the same data but using an additional channel to communicate species level differences. Again fit a linear model to the data but this time one for each species, and add additional theme and labeling modicitations.
ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(x = "Iris Sepal Length", y = "Iris Petal Length",
title = "Relationship between Petal and Sepal Length",
subtitle = "Species level comparison") +
theme(legend.position = "bottom") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())