A list of packages required.
Use “install.packages(”packagename") if you have not previously loaded these packages into your R.
“swiss” is a dataset describing several socioeconomic factors within french-speaking regions in Switzerland in the 1800s:
Table 1, List of variables in swiss
| Variable Name | Variable Type | Variable Description |
|---|---|---|
| Fertility | Numeric | Fertility Ig, ‘common standardized fertility measure’ |
| Agriculture | Numeric | Agriculture % of males involved in agriculture as occupation |
| Examination | Numeric | Examination % draftees receiving highest mark on army examination |
| Education | Numeric | Education % education beyond primary school for draftees. |
| Catholic | Numeric | Catholic % ‘catholic’ (as opposed to ‘protestant’). |
| Infant.Mortality | Numeric | Infant.Mortality live births who live less than 1 year. |
Sometimes you might want to arrange multiple related figures into a panel of plots. It’s very easy to do this with ggplot2.
Imagine I want to several plots showing the impact of multiple socioeconomic variables on the fertility index in Switzerland. First, I need to create my different plots. For the sake of this exercise I have chosen to examine the effect of the variables: Agriculture, Education & Catholic. Select the tabs below to see the code and the output of each plot. If this code seems scary for you - take a look back at “Tutorial_GGplot2 basics”.
#make the plot
agriculture <- ggplot(data = swiss, aes(x = Agriculture, y = Fertility)) +
geom_point(shape = 21, size = 2, color = "skyblue3", fill = "skyblue") +
geom_smooth(method = lm, color = "black", size = 0.7, linetype = "dashed") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "black"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 12)) +
xlab("Percent Men In Agriculture") +
ylab("Fertility Index") +
xlim(0, 60) +
ylim(0, 100)
#call the plot
agriculture#make the plot
education <- ggplot(data = swiss, aes(x = Education, y = Fertility)) +
geom_point(shape = 21, size = 2, color = "palegreen3", fill = "palegreen3") +
geom_smooth(method = lm, color = "black", size = 0.7, linetype = "dashed") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "black"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 12)) +
xlab("Percent Educated Beyond Primary Level") +
ylab("Fertility Index") +
xlim(0, 60) +
ylim(0, 100)
#call the plot
education#make the plot
catholic <- ggplot(data = swiss, aes(x = Catholic, y = Fertility)) +
geom_point(shape = 21, size = 2, color = "plum3", fill = "plum1") +
geom_smooth(method = lm, color = "black", size = 0.7, linetype = "dashed") +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "white", colour = "black"),
axis.title = element_text(size = 12),
axis.text = element_text(size = 12)) +
xlab("Percent Catholic") +
ylab("Fertility Index") +
xlim(0, 60) +
ylim(0, 100)
#call the plot
catholicThe most basic way to arrange plots in a multi-panel plot is to use the package gridExtra. Here we specify the plots we want to arrange in a list and we can also specify how many rows and columns there are available for the arrangement. This is useful because without these arguments grid.arrange will pick a default organisation of the plots, which in this case results in the figures being squashed horizontally.
Another option is to use the package cowplot, which is what we will focus on here. You can get more detail on cowplot and it’s functions here: https://wilkelab.org/cowplot/articles/.
This is the most basic, default cowplot for my three plots. If I wanted to keep the same arrangement and add a little more detail, like labels or make sure that the horizontal axes are all aligned I can add in further arguments as below.
plot_grid(agriculture, education, catholic,
#provide labels for plots
labels = c('A', 'B', 'C'),
#ensure plots are aligned on the horizontal axis
align = "h",
ncol = 2, nrow = 2)Looks good, but there’s still a lot of white space in the corner of our figure that we may want to get rid of. The package ggpubr is a wrapper for cowplot and is designed to produce publication-ready figures. We can use the function ggarange() within this package to adjust the separate, nested, segments of the panel.
ggarrange(agriculture,
# Create a second row after calling the first plot where we can define the aspects of this smaller element.
ggarrange(education, catholic, ncol = 2, labels = c("B", "C")),
# State the total number of rows.
nrow = 2,
# Label the first plot
labels = "A") Alternatively, you might have a table containing some model outputs or descriptive statistics that you want to include as part of the plot (particularly if you are making a poster).
#Create columns
variable <- factor(c("Agriculture","Education","Catholic"))
mean <- c(mean(swiss$Agriculture), mean(swiss$Education), mean(swiss$Catholic))
#Create table
table <- data.frame(first = variable, second = mean)
#Rename table columns
colnames(table) <- c("Variable", "Mean")
#Change the table into a plottable object
paneltable <- ggtexttable(table, rows = NULL, theme = ttheme("classic"))
#Create the plot arrangement
ggarrange(agriculture, education, catholic, paneltable,
ncol = 2, nrow = 2,
labels = c("A", "B", "C", "D"))This is obviously a very basic intro to the way you can arrange your plots! If you would like to go into more detail you can access more information on Google (obvs!) as well at these locations: