In the first part of the ggplot series, we have talked about the geom and the scale layers. In visualization terms, faceting means juxtaposing several plots together. This is especially useful when we want to compare several categories together. Now, let’s jump into several ways of faceting in ggplot.
facet_grid() is used for creating subplots. Subplots can be created when the plots we want to create share common axes.
Usage: facet_grid(rows=NULL, cols=NULL, scales = 'fixed', space = 'fixed') .
- rows, cols –> define how we would like to facet our graph
- scales =
- ‘fixed’ = scales shared across all facets
- ‘free_x’ = scales vary across rows
- ‘free_y’ = scales vary accross columns
- ‘free’ = scales vary accross both rows and columns
- space =
- ‘fixed’ = all panels have the same size.
- ‘free_x’ = the panel’s width will be proportional to the length of the x scale.
- ‘free_y’ = the panel’s height will be proportional to the length of the y scale.
- ‘free’ = both panel’s height and width will vary.
Various examples of using facet_grid() taken from ggplot2.tidyverse.org:
- defining the rows
- basic formula: p + facet_grid(rows = vars(drv))
- shortcut formula : p + facet_grid(drv ~ .)
- defining the cols
- basic formula: p + facet_grid(cols = vars(cyl))
- shortcut formula: p + facet_grid(. ~ cyl)
- defining the rows and cols
- basic formula: p + facet_grid(vars(drv), vars(cyl))
- shortcut formula: p + facet_grid(drv ~ cyl)
- defining the rows
Wraps a 1d sequence of panels into 2d. It’s a little bit similar to that of
facet_grid is determined by 2 variables (columns), while
facet_wrap() is only determined by 1 variable (column). Don’t forget to wrap the column used for faceting inside
vars() or using
Usage: facet_wrap(vars(facets), nrow = NULL, ncol = NULL).
- facets = the column name that will be used for defining the facet groups. It can be written as vars(column_name) or ~ column_name .
- nrow, ncol = Number of rows and columns.
Below are the examples of plot using various ncols
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
# Use vars() to supply faceting variables:
p + facet_wrap(vars(class))
3. Other Ways to Facet
facet_wrap(), we can also create a subplot in R using
mfrow is a vector of two (2) elements where the 1st element indicates the number of rows and the second element indicates the number of columns. However, its usage is a bit different compared to
par(mfrow) is used in basic R and instead of adding it as a layer, we execute
par(mfrow) before calling the
par(mfrow = c(1,3)) # 1 row and 3 columns.
plot(hc_complete, main = 'Complete Linkage')
plot(hc_single, main = 'Single Linkage')
plot(hc_average, main = 'Average Linkage')
ggparcoord() is mostly used for creating a basic parallel plot, where each variable plotted as a z-score transformation. To use
ggparcoord() we must first install and load the library
ggparcoord(data, columns = 1:ncol(data), groupcolumn = NULL) .
Notes that it does not use the base layer
ggplot(). Instead, it directly uses
- columns = a vector of variables (either names or indices) to be axes in the plot.
- groupcolumn = a single variable to group (color) by.
# Parallel coordinates plot using GGally
# All columns except am
group_by_am <- 9
my_names_am <- (1:11)[-group_by_am]
# Basic parallel plot - each variable plotted as a z-score transformation
ggparcoord(mtcars, my_names_am, groupColumn = group_by_am, alpha = 0.8)
Phew, we just finished learning about facet in ggplot. Hope it’s useful for you all. Stay tuned because there are more ggplot posts coming. See you 🙂