Faceting in ggplot2
   get_help() docs


Description

All functions documented here are part of the {ggplot2} package, which is part of the {tidyverse}.

The {ggplot2} package provides two useful functions for creating paneled, i.e. faceted plots:

To use these functions, you need to either first load the {ggplot2} library, or always use the function with ggplot2::() notation.

# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation, for example with facet_wrap()
ggplot2::facet_wrap()

Conceptual Usage

# Most basic usage:

# We do NOT use aes() to specify variables for faceting. Instead we use vars():
facet_wrap(vars(tibble variable to facet across))

facet_grid(rows = vars(tibble variable to facet across rows),
           cols = vars(tibble variable to facet across columns))

# Or, facet_grid with one variable:
facet_grid(rows = vars(tibble variable to facet across rows))
facet_grid(cols = vars(tibble variable to facet across columns))

Examples

The examples below use a modified version of the msleep dataset, where NA values have been removed from columns vore and conservation using tidyr::drop_na(). Learn more about the msleep with get_help("msleep").

# Modify msleep for examples:
msleep %>%
  tidyr::drop_na(vore, conservation) -> msleep_cleaned # Plots are made with `msleep_cleaned`

# Show dataset:
msleep_cleaned
## # A tibble: 40 × 11
##    name         genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt
##    <chr>        <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>
##  1 Mountain be… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA      
##  2 Greater sho… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029
##  3 Cow          Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423  
##  4 Northern fu… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA      
##  5 Dog          Canis carni Carn… domesticated        10.1       2.9       0.333  13.9  0.07   
##  6 Goat         Capri herbi Arti… lc                   5.3       0.6      NA      18.7  0.115  
##  7 Guinea pig   Cavis herbi Rode… domesticated         9.4       0.8       0.217  14.6  0.0055 
##  8 Grivet       Cerc… omni  Prim… lc                  10         0.7      NA      14   NA      
##  9 Chinchilla   Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5  0.0064 
## 10 Star-nosed … Cond… omni  Sori… lc                  10.3       2.2      NA      13.7  0.001  
## # … with 30 more rows, and 1 more variable: bodywt <dbl>


# Use facet_wrap with default layout to panel scatterplot by vore: Each panel represents a vore.
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore))


# Use argument `nrow` to specify how many panel rows facet_wrap should plot.
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore), nrow = 1) # All panels will be laid out in ONE ROW


# Or, use argument `ncol` to specify how many panel columns facet_wrap should plot.
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore), ncol = 1) # All panels will be laid out in ONE COLUMN


# Use the `scales` argument to allow x-axis limits to vary acrcoss panels depending on panel data ranges
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore), scales = "free_x") # Free the x scales to vary based on data - now panels have different x-axis ranges


# Use the `scales` argument to allow y-axis limits to vary acrcoss panels depending on panel data ranges
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore), scales = "free_y") # Free the y scales to vary based on data - now panels have different y-axis ranges


# Use the `scales` argument to allow BOTH x- and y-axis limits to vary acrcoss panels depending on panel data ranges
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  facet_wrap(vars(vore), scales = "free") # Free the BOTH x and y scales to vary based on data - now panels have different axis ranges


# Use facet_grid to make a paneled plot of conservation and vore
# Note that some panels are empty, meaning there are no data points wiht that combination of those vore/conservation categories in msleep.
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, vore panels are in rows, and conservation panels are in columns
  facet_grid(rows = vars(vore),
             cols = vars(conservation))


# Use facet_grid to make a paneled plot of conservation and vore, reversed
# Note that some panels are empty, meaning there are no data points wiht that combination of those vore/conservation categories in msleep.
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, conservation panels are in rows, and vore panels are in columns
  facet_grid(rows = vars(conservation),
             cols = vars(vore))


# Free both axis scales with `scales = "free". This can also be done as `scales = "free_x"` or `scales = "free_y"` to free only one axis
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, conservation panels are in rows, and vore panels are in columns
  facet_grid(rows = vars(conservation),
             cols = vars(vore), 
             scales = "free") # allow both X and Y panel axes ranges to vary based on panel's data


# Switch y facet label locations in a faceted plot (works with either facet_wrap or facet_grid)
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, conservation panels are in rows, and vore panels are in columns
  facet_grid(rows = vars(conservation),
             cols = vars(vore), 
             switch = "y") # This argument switches y facet labels to be on the LEFT


# Switch x facet label locations in a faceted plot (works with either facet_wrap or facet_grid)
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, conservation panels are in rows, and vore panels are in columns
  facet_grid(rows = vars(conservation),
             cols = vars(vore), 
             switch = "x") # This argument switches x facet labels to be on the BOTTOM


# Switch x and y label locations in a faceted plot (works with either facet_wrap or facet_grid)
ggplot(msleep_cleaned) + 
  aes(x = sleep_rem, 
      y = awake, 
      color = vore) + 
  geom_point() + 
  # Here, conservation panels are in rows, and vore panels are in columns
  facet_grid(rows = vars(conservation),
             cols = vars(vore), 
             switch = "both") # This argument switches BOTH x and y facet labels to be on the bottom and left, respectively