Loading required libraries.
library(ggplot2)
library(ggthemes)
This graphic is a traditional stacked bar chart. This graphic works on the mpg dataset, which is built into the ggplot2 library. This means that you can access it simply by ggplot(mpg, ….). There is one modification above default in this graphic, I renamed the legend for more clarity.
library(ggplot2)
Transmission <- mpg$trans
gg <- ggplot(mpg,aes(class,fill = Transmission))
gg + geom_bar()
This boxplot is also built using the mpg dataset. Notice the changes in axis labels, and an altered theme_XXXX
p1 <- ggplot(mpg,aes(manufacturer,hwy))
plot1 <- p1 + geom_boxplot() + coord_flip() + labs(x = "Manufacturer", y = "Fuel Efficiency")
plot1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
This graphic is built with another dataset diamonds a dataset also built into the ggplot2 package. For this one I used an additional package called library(ggthemes) check it out to reproduce this view.
library("ggthemes")
p2 <- ggplot(diamonds, aes(x = price, ..density.., fill = cut, colour = cut)) + geom_density(alpha = 0.1)
p2 + theme_economist() + labs(x = "Diamond Price(USD)", y = "Density") + ggtitle("Diamond Price Density")
For this plot we are changing vis idioms to a scatter plot framework. Additionally, I am using ggplot2 package to fit a linear model to the data all within the plot framework. Three are edited labels and theme modifications as well.
p3 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) +
geom_point() + geom_smooth(method = "lm")
p3 + theme_minimal() + labs(x = "Iris Sepal Length", y = "Iris Petal Length") + ggtitle("Relationship between Petal and Sepal Length")
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 I fit a linear model to the data but this time one for each species, and add additional theme and labeling modicitations.
p4 <- ggplot(iris, aes(Sepal.Length, Petal.Length,colour = Species)) + geom_point() + geom_smooth(se = FALSE, method = "lm")
p4 + theme_tufte() + labs(x = "Iris Sepal Length", y = "Iris Petal Length") + ggtitle("Relationship between Petal and Sepal Length", subtitle = "Species Level Comparison")