In this tutorial, we will see how to make a grid of boxplots using facet_grid(). Such a grid may be useful when your data set contains several categorical predictor variables, and displaying the data in a single graph makes it hardly comprehensible. If you are not so familiar with boxplots or facet_grid(), have a quick look at these two pages:


We will plot the temperatures recorded daily in 2018 and 2019 at two Norwegian locations, Lygra and Østerbø, in a multiple boxplot. In this plot, each box represents the temperatures for a given month. We will thus have three categorical variables: month, year and location, and one response variable temperature. Here is the code for the dataframe:

# dataframe
df <- data.frame(location, year, month, temperature)
# structure of the dataframe
str(df)
## 'data.frame':    1460 obs. of  4 variables:
##  $ location   : Factor w/ 2 levels "Lygra","Østerbø": 1 1 1 1 1 1 1 1 1 1 ...
##  $ year       : Factor w/ 2 levels "2017","2018": 1 1 1 1 1 1 1 1 1 1 ...
##  $ month      : Factor w/ 12 levels "Jan","Feb","Mar",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ temperature: num  2.4 0.3 2.8 2.3 -2.4 2.9 2.6 4.6 6.8 5.3 ...



Our plan is to make a matrix displaying 4 panels, each of which is a (multiple) boxplot. In these boxplots, the predictor variable month and the response variable temperature shall be plotted on the X- and Y-axis, respectively. Eventually the matrix shall show location in columns and year in rows. To obtain this matrix, we must:

Here is the code, and the corresponding faceted plot:

ggplot(df, aes(x = month, y = temperature)) +
  geom_boxplot() + 
  facet_grid(year~location)



If the plan was to set up a matrix with location in rows and year in columns, we should have inverted the variables in facet_grid():

ggplot(df, aes(x = month, y = temperature)) +
  geom_boxplot() + 
  facet_grid(location~year)



Improving the look

You may improve the look of a grid by tuning the labels of the matrix. This is further explained HERE.

Alternative plot

This data set may be alternatively plotted in the form of a grid of clustered/grouped boxplot. HERE is a tutorial for making such a plot.