Section 7 Other graphics packages
I won’t attempt to cover
ggplot2 in any detail here. For an easy-to-read guide, I recommend Chang (2013). For a shorter guide, you could try Chapter 8 of the Cookbook for R, which is by the same author and presented in a similar style, but less comprehensive. The following examples should give you some idea how
ggplot2 works. The basic ideas are as follows.
- The data you want to plot must be in a dataframe. Start off with a
ggplotcommand, specifying the dataframe you are using.
- Columns in the dataframe are mapped to ‘aesthetics’ using the
aesargument. You might map one column to the \(x\)-axis, another column to the \(y\) axis, a third column (a factor variable) to the colour and so on.
- ‘Layers’ can be added to plots, e.g. one layer for the points, another for lines, another specifying axes labels etc. For example adding the layer
geom_point()will add points to your existing plot, using the columns mapped to the \(x\) and \(y\) axes;
- Plots can be stored as objects, and added to/modified later on.
7.1.1 A scatterplot
library(ggplot2) ggplot(data = mtcars, aes(x = wt, y = mpg, colour = as.factor(cyl), shape = as.factor(cyl))) + geom_point(size = 2) + labs(x = "Weight (lb/1000)", y = "Miles/ (US) gallon", colour = "number of\ncylinders", shape = "number of\ncylinders")
Here we have ‘initialised’ the scatter plot with the
ggplot() command, and then used
geom_point() to add the points. We don’t actually need any arguments in
geom_point(), because we have already ‘mapped’
wt to the \(x\)-axis and
mpg to the \(y\)-axis with the
aes() argument in the
ggplot() command, but I have chosen to include a
size argument to draw larger points.
7.1.2 Box plots
ggplot(data = na.omit(mvscores), aes(x = innings, y = runs, colour = captain)) + geom_boxplot() + scale_x_discrete(labels=c("First innings", "Second innings")) + labs(x = "", y = "runs scored")
7.1.3 Comparing histograms
labels <- c(no = "Not played as captain", yes = "Played as captain") ggplot(data = na.omit(mvscores), aes(x = runs, y = ..density..)) + geom_histogram(binwidth = 10, center = 5, fill = "white", col = "black") + facet_wrap(~ captain, labeller = labeller(captain = labels)) + labs(x = "runs scored")
7.1.4 Scatterplot with log axis
ggplot(data = medals, aes(x = population / 1000000, y = total))+ geom_point() + scale_x_log10(breaks = c(1, 10, 100, 1000)) + labs(x = "population (millions)", y = "total number of medals won")
7.1.5 Bar chart
ggplot(data = inequality, aes(x = factor(country, levels = country[order(gini)]), y = gini)) + geom_col(fill = "grey") + coord_flip() + geom_hline(yintercept = c(0.1, 0.2, 0.3, 0.4), col = "white") + labs(y = "gini coefficent", x = "") + theme_classic() + theme(axis.line=element_blank())
plotly, we can produce interactive graphics, where, for example, we can hover the cursor over points to get more information. Here, we produce a scatterplot, where hovering the cursor over a point will display the model of car (the row name in the
library(plotly) plot_ly(mtcars, x = ~wt, y =~mpg, type = 'scatter', mode = 'markers', hoverinfo= 'text', text = ~rownames(mtcars))
Chang, Winston. 2013. R Graphics Cookbook. Farnham: O’Reilly.