Packages Loaded

A list of packages required.

Use “install.packages(”packagename") if you have not previously loaded these packages into your R.

Load the data needed

“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.

Make a multi-panel plot

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”.

Multiple plots

Make the panels

The 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.

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.

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).

Even More Fun!

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:

  1. http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
  2. https://wilkelab.org/cowplot/index.html
  3. http://www.ggplot2-exts.org/gallery/
  4. https://www.r-graph-gallery.com/
  5. https://r-graphics.org/