#Load package
library("mgrtibbles")
#Set seed for random sampling
set.seed("483")
#mushroom_tbl tibble for demonstration
<- mgrtibbles::mushroom_tbl |>
mushroom_tbl #Random sample of 150 rows
::slice_sample(n = 300, replace=FALSE)
dplyr#Reset random seed to normal operation
set.seed(NULL)
Facetting

Facetting splits a plot into multiple panels using one or two metadata variables. This allows you to separate groups so you can more easily see the differences within and between the groups.
Dataset
We’ll create scatter plots similar to those in the geom_point()
chapter, so we’ll load the mushroom_tbl
data from the mgrtibbles package (hyperlink includes install instructions). We will extract a random sample of 300 rows with slice_sample()
.
Facet grid
The function ggplot2::facet_grid()
allows you to split/facet a plot by columns and rows. This can be used if you have one or two discrete variables you want to facet by.
It is important to note that with this method the scales are fixed (i.e. all the panels have the same scale) by default. To learn more and edit this see the facet scales section.
facet_grid()
reference webpage
Facet grid by columns
Create a scatter plot of stem_height (y) against stem_width (x).
We’ll facet the data by the variable class
. We’ll facet by columns (i.e. the facet panels are separated by rows on the plot) with dplyr::facet_grid(cols = dplyr::vars(class))
.
Note: We have to place the variable/column name into the dplyr::vars()
function for this.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet grid class by columns
::facet_grid(cols = dplyr::vars(class)) ggplot2
Facet grid by rows
Create a scatter plot of stem_height (y) against stem_width (x).
We’ll facet the data by the variable cap_color
. We’ll facet by rows (i.e. the facet panels are separated by rows on the plot) with ggplot2::facet_grid(rows = dplyr::vars(cap_color))
.
Note: We have to place the variable/column name into the dplyr::vars()
function for this.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet grid cap_color by rows
::facet_grid(rows = dplyr::vars(cap_color)) ggplot2
Facet grid by columns and rows
We’ll combine the column and row facetting of the 2 above plots into one plot.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet grid class by columns and cap_color by rows
::facet_grid(cols = dplyr::vars(class), rows = dplyr::vars(cap_color)) ggplot2
Facet wrap
The function ggplot2::facet_wrap()
will wrap panels over multiple rows. The number of rows/columns to wrap around can be set with nrow=
/ncol=
. This is the preferred method when you are facetting by one discrete variable.
It is important to note with this method the scales are fixed (i.e. all the panels have the same scale). To learn more and edit this see the facet scales section.
facet_wrap()
reference webpage
Default facet wrap
Create a scatter plot of stem_height (y) against stem_width (x).
We’ll facet the data by the variable cap_color
. This will be carried out with ggplot2::facet_wrap(dplyr::vars(cap_color))
.
Note: We have to place the variable/column name into the dplyr::vars()
function for this.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet wrap cap_color
::facet_wrap(dplyr::vars(cap_color)) ggplot2
Facet wrap with number of columns
The number of columns to wrap the panels around can be chosen with the ncol=
option.
Create a scatter plot of stem_height (y) against stem_width (x).
We’ll facet the data by the variable cap_color
. Additionally, we’ll set the number of columns to wrap the panels around to 2 with ncol=2
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet wrap cap_color with 2 wrapping columns
::facet_wrap(dplyr::vars(cap_color), ncol=2) ggplot2
Facet wrap with number of rows
The number of rows to wrap the panels around can be chosen with the nrow=
option.
Create a scatter plot of stem_height (y) against stem_width (x).
We’ll facet the data by the variable cap_color
. Additionally, we’ll set the number of rows to wrap the panels around to 2 with nrow=2
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet wrap cap_color with 2 wrapping rows
::facet_wrap(dplyr::vars(cap_color), nrow=2) ggplot2
Facet scales
By default both ggplot2::facet_wrap()
and ggplot2::facet_grid()
will have their scales be “fixed”. This means that the scales will be the same for each panel. In other words the panels in the same row share the same scales which are identical across the columns. This also means the panels in the same column share the same scales which are identical across the rows.
Having the scales as “fixed” allows you to visualise the difference between the different facetting variables. However, this can make it difficult to view the difference within a panel. This can be seen in the above plots with the colour_cap
variables: pink, purple, buff, green, and blue. In these variables the points are very close together (overclustered) so it is hard to differentiate between the points.
To change the scales we can use the options scales=
with one of the following:
"fixed"
: This is the default with both scales being fixed."free"
: This allows both axes scales to be free."free_x"
: The x axis scale is free but the y axis scale is fixed."free_y"
: The y axis scale is free but the x axis scale is fixed.
Free scales for facet_grid()
Create the same plot as that made in the facet grid by columns and rows and set the scales to free. This is carried out with the option scales="free"
within the function ggplot2::facet_grid()
.
In the below plot notice that the scales for each row and each column are “free”. In other words the scales have been chosen to fit the data in each row and in each column. It is important to note that the scales for each panel are not free.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet grid class by columns and cap_color by rows
#Set scales to free
::facet_grid(cols = dplyr::vars(class), rows = dplyr::vars(cap_color),
ggplot2scales="free")
Free scales for facet_wrap()
Create the same plot as that made in the Default facet wrap and set the scales to free. This is carried out with the option scales="free"
within the function facet_wrap()
.
In the below plot notice that the scales for each panel are “free”. In other words the scale have been chosen to fit the data in each panel.
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height)) +
ggplot2::geom_point() +
ggplot2#Facet wrap cap_color
::facet_wrap(dplyr::vars(cap_color), scales="free") ggplot2
Facets as an aesthetic
Facets become especially useufl when you have a lot of variables you would like to represent as aesthetics. There are no limits to the the number of facets you can use, unlike colours and shapes.
Let’s create a final scatter plot plot with the mushroom_tbl
tibble. The aesthetics we will set are:
x=stem_width
y=stem_height
colour=cap_shape
shape=class
Additionally, we will facet with gill_attachment
and gill_color
|>
mushroom_tbl ::ggplot(aes(x = stem_width, y = stem_height, color = cap_shape, shape = class)) +
ggplot2::geom_point() +
ggplot2#Facet grid class by columns and cap_color by rows
::facet_grid(cols = dplyr::vars(gill_attachment), rows = dplyr::vars(gill_color)) +
ggplot2#Labels
::labs(title="Scatter plot showing features of mushrooms",
ggplot2subtitle="Facetted by gill colour (rows/x axis) and gill attachment (columsn/y axis)",
x = "Stem width", y = "Stem height",
color = "Cap shape", shape = "Edibility")