Introduction to facets; facet_grid and facet_wrap

The aim of this superguide is to explain the two types of facets that can be created when using Ggplot2 umbrella package: facet_grid and facet_wrap. Both of these packages allow us to create multi-panels.

We will first create a series of commands which will enable us to illustrate the starting point of the data we will be working on. Then we will apply both packages and give examples on how it looks like to utilise them, the majority of the arguments that are included in each one and finally a brief conclusion on when it is suggested to go for one or the other.

Before we start using the packages:

Install packages ggplot2,here and tidyverse. After that run the library:

library(ggplot2)
library(here)
## here() starts at C:/Users/laura/OneDrive/Documents
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.0     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

After that we will load our data: participants_new data.

Note: This data has been edited for illustration purposes and a subset of the data will now be utilised.

participants_new <- read_csv(here("Data", "participants_new.csv"))
## New names:
## * `` -> ...1
## Rows: 16 Columns: 3
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): district
## dbl (2): ...1, participants
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.

And now, hands on with the matrix!

All about facet_grid

facet_grid allows to create multi-panels from matrix that are defined by the same row and column faceting variables. Let’s see this in action! I will be using the same line of code looking at the population relationship we are looking at, and I will be along adding this new command, specifying I want the graph to be created by cut:

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip()

Now we want to take a look at how cut comes into the equation facet_grid. As you will see in my line of code I am asking the facets to be faceting by participants.

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip() + facet_grid(~participants)

We will intend to find a way for the x axis to change and visually look better while learning the commands.

space

space allows to create different scales on both the x and/or y axis. There are four commands which are: “free”, “free_x” and “free_y”. The last 4 is “fixed” and is added by default.

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip() + facet_grid(~participants, space = "free_x")

We can see how despite it could adjust on the x axis we are still unable to actually see those values. Let’s see what else we can try

margins

Adding margins = TRUE will automatically create a new facet. Let’s see how this looks like:

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip() + facet_grid(~participants, margins = TRUE)

We can identify that we still are unable to read the actual values per district, despite it was given more space to all facets its also barely noticeable due to the distribution.

scale_x/y_continuous

scale_x/y_continuous will enable us to change in this case the x axis. Because I have initially used a coord_flip x is actually read as y and viceversa. In this case as I wanted to change the title which was supposed to be in the x axis I will asking it to send it to the “right”

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip() + facet_grid(~participants) + scale_y_continuous(position = "right")

Now let’s move into facet_wrap and some differences in between both facet options

The facet_wrap allows you to arrange 1d panels into 2d. This, differing from facet_grid which is essentially a 2d facet looking to be transformed into multiple 2d facets. Therefore, is much easier when you have one variable with many levels to be managed by facet_wrap. Furthermore, when there are various factor levels.

Let’s now learn about the facet_wrap commands

nrows and ncolumns

Unlike facet_grid and with the purpose of being more flexible in managing scales between both of these facet options, both of these commands allow you to decide as the name says the number of rows and columns. This will make your graph will be essentially the same but be wider or longer, and potentially in some instances easier to understand and interpret.

Let’s look at the example with the same matrix:

ggplot(data = participants_new, aes(x = district, y = participants)) + geom_point() + coord_flip() + facet_wrap(~participants, nrow = 2)

A little extra

I will get adding a new command for colour per district which will facilitate identifying which district does the data come from.

ggplot(data = participants_new, aes(x = district, y = participants, colour = district)) + geom_point() + coord_flip() + facet_wrap(~participants, nrow = 2)

Drops

Unlike facet_grid, facet_wrap wont allow you to visualise facets that dont have data even if they are included in the facets you have asked to visualise. In order to ignore this command you an use the drop command. As a default it drops this data therefore we will be using instead of drop = TRUE, drop = FALSE.

ggplot(data = participants_new, aes(x = district, y = participants, colour = district)) + geom_point() + coord_flip() + facet_wrap(~participants, nrow = 2, drop = FALSE)

We had nothing to observe as all districts have participant’s but if there was/were some that didn’t, they would be appearing as a new facet.

scales

Similar to scale under facet_grid, we will be using “free”, “free_x” and “free_y”. The last 4 is “fixed” and is added by default.This will allow to free either axis and the variables in each facet.

Note: Allowing to free the scales must be used carefully, as the interpretation of the data will generally not be consistent along facets. Therefore, it wouldn’t generally be used for reporting purposes.

ggplot(data = participants_new, aes(x = district, y = participants, colour = district)) + geom_point() + coord_flip() + facet_wrap(~participants, nrow = 2, scales = "free_x")

strip.position

Is the replacement version of facet_grid: switch and is essentially allowing to control the label positions. You can use “right”, “left” and “bottom”. As a default labels will be on “top”

In this case we will intending to move the strips to the bottom.

ggplot(data = participants_new, aes(x = district, y = participants, colour = district)) + geom_point() + coord_flip() + facet_wrap(~participants, nrow = 2, strip.position = "bottom")

References

NDIA. (2021, March 31). Data downloads. NDIS. https://data.ndis.gov.au/data-downloads

Wickham, H., Navarro, D., & Pedersen, L. (2016). Faceting. ggplot2. https://ggplot2-book.org/facet.html?q=facet_wrap#facet-wrap

Wickham, H., & Grolemund, G. (2017). Data visualisation. R for Data Science. https://r4ds.had.co.nz/data-visualisation.html?q=facet_gri#facets