This is a powerful library, which has revolutionized the way graphics are plotted by incorporating a "grammar of graphics", which allows different features to combined and overlaid. This approach has inspired a ports to Python and other languages, such as Julia.
Let's start by loading Fisher's classic data set on three species of Iris.
library(ggplot2)
data(iris)
head(iris)
In this data set we have morphometric data on flower shape from three species. In ggplot, the main plotting command is ggplot, which takes a data frame and sets up an aesthetic, which is the mapping between the variables. This mapping can then be visualized by applying a geometry. E.g.,
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width))+geom_point()
In this case, we use set up a relationship between Sepal.Length and Sepal.Width using the aes function and then plotted it using a scatter plot (geom_point function in ggplot). We can easily supply additional aesthetics, such as species:
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()
In this example, we added an aesthetic that color-codes each species, though we could also use something like shape
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species))+geom_point()
You can find out more about available aesthetics by asking R directly:
?geom_point
Geometries can also be changed, allowing the same data set to be visualized in different ways, by changing which variables you want to plot
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_point()
You can change geometries to make different types of plots
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_boxplot()
For instance, the boxplot can summarize data into quantiles and medians, which is helpful when you have too many points to visualize. In this manner you can play around with different types of visualizations and choose which one looks best.
Technically geom_point is a layer. You can add them together easily to make more complex plots
ggplot(iris, aes(x=Species, y=Sepal.Length))+geom_boxplot()+geom_jitter()
In this example, we added a scatterplot (slightly jittered to prevent overplotting) on top of a boxplot summarizing the data.
Facets are panels that allow you to subdivide the data
ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length))+geom_point()+facet_grid(.~Species)
In this case, we split the plot into three components oriented along the x-axis
What is the effect of
ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length))+geom_point()+facet_grid(Species~.)
The axes can be transformed to suit your needs, e
ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length))+geom_point()+scale_x_continuous(limits=c(2,3),"x axis")+scale_y_reverse()
You can change visual aspects of the plot (how text is rendered, what is colored using the
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+geom_point()+theme_bw()
There are, of course, lots of other packages for plotting data in R, and they may make certain kinds of plots easier.
require(graphics)
mosaicplot(Titanic, main = "Survival on the Titanic")