Faceting in ggplot2

Robert Schlegel

Problem

  • What if the data have multiple categories?
  • What if dots and lines aren’t enough?
  • What if we need to combine plots?

Solution

  • We add facets to plots with just one function
  • By using different geom_*() we create different plots
  • ggpubr helps us to combine many different plots

Setup

We will need the following three packages for the examples in these slides.

library(tidyverse) # Contains ggplot2

library(ggpubr) # Helps us to combine figures

library(palmerpenguins) # Contains the dataset

Basic plot

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm")

Facet wrap

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  # Can take one or two column names (e.g. island~species)
  facet_wrap(~species)

Facet grid

ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm") +
   # Takes two or more column names (e.g. species+sex~island)
  facet_grid(species~island)

Create plot object

# Assign the ggplot2 code to an object name
lm_1 <- ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth(method = "lm") +
  labs(x = "Body mass (g)", y = "Bill length (mm)", colour = "Species")

# Visualise the plot by calling the object
lm_1

Non-linear model

nlm_1 <- ggplot(data = penguins,
       aes(x = body_mass_g, y = bill_length_mm, colour = species)) +
  geom_point() +
  geom_smooth() +
  labs(x = "Body mass (g)", y = "Bill length (mm)", colour = "Species")
nlm_1

Histogram

histogram_1 <- ggplot(data = penguins, 
                      # NB: There is no y-axis value for histograms
                      aes(x = body_mass_g)) + 
  geom_histogram(aes(fill = species), position = "stack", binwidth = 250) +
  # NB: We use 'fill' here rather than 'colour'
  labs(x = "Body mass (g)", fill = "Species")
histogram_1

Boxplot

box_1 <- ggplot(data = penguins, 
                # Why 'as.factor()'?
                aes(x = as.factor(year),
                    y = body_mass_g)) + 
  geom_boxplot(aes(fill = species)) +
  labs(x = "Year", y = "Body mass (g)", fill = "Species") 
box_1

Combine plots

# List the names of the plot objects to combine 
grid_1 <- ggarrange(lm_1, nlm_1, histogram_1, box_1,
                    # Set number of rows and columns
                    ncol = 2, nrow = 2,
                    # Label each figure
                    labels = c("a)", "b)", "c)", "d)"),
                    # Create common legend
                    common.legend = TRUE,
                    # Set legend position
                    legend = "bottom")
grid_1

Save plots

# Different file types
ggsave(plot = grid_1, filename = "figures/grid_1.pdf")
ggsave(plot = grid_1, filename = "figures/grid_1.png")
ggsave(plot = grid_1, filename = "figures/grid_1.eps")

# Change dimensions
ggsave(plot = grid_1, filename = "figures/grid_1.png", 
       width = 10, height = 8)

# Change DPI
ggsave(plot = grid_1, filename = "figures/grid_1.png", dpi = 600)