stickylabeller

The stickylabeller package helps you label the facets in your ggplot2 plots using glue.

Installation

Install stickylabeller from my R-Universe:

install.packages("stickylabeller", repos = c(
  "https://jimjam-slam.r-universe.dev",
  "https://cloud.r-project.org"))

Use

The package has just one function: label_glue. Give it a string template to be processed by glue, and it’ll return a labelling function that you can pass to facet_*:

library(ggplot2)
library(stickylabeller)

# here's some example data: some random points in groups
mydf <- data.frame(
  x = 1:90,
  y = rnorm(90),
  red = rep(letters[1:3], 30),
  blue = c(rep(1, 30), rep(2, 30), rep(3, 30)))

# and here's a labelled plot!
ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    vars(red, blue),
    labeller = label_glue("Red is {red}\nand blue is {blue}"))

facet_wrap labelled with two facet column values

ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_grid(
    vars(red), vars(blue),
    labeller = label_glue(
      "Red is {red}",
      "Blue is {blue}"))

facet_grid labelled with one facet column value on each margin

Your label_glue labeller can refer to any of the data frame columns included in the facetting formula. It can also use those columns in expressions, like:

my_labeller <- label_glue("Red is {toupper(red)}\nand blue is {blue}")

Numbering sequential facets

As well as the columns you include in the facetting specification, stickylabeller includes a few helper columns:

So you can automatically number your facets like:

ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    vars(red, blue),
    labeller = label_glue("({.l}) Red is {toupper(red)}\nand blue is {blue}"))

facet_wrap labelled with sequential lowercase letters: (a), (b), (c) and so on

Limitations

Including summary statistics in facet labels

As of stickylabeller 1.0, label_glue now accepts a summary_data argument. This accepts a data frame of pre-computed summary data for the facets; it should include the facet grouping columns, plus any other variables you’d like to reference in the templates.

For example:

library(magrittr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

# summarise the data
mydf_stats = mydf |>
  group_by(red, blue) |>
  summarise(
    mean_y = sprintf("%#.2f", mean(y)),
    sd_y = sprintf("%#.2f", sd(y))) |>
  ungroup()
#> `summarise()` has grouped output by 'red'. You can override using the `.groups`
#> argument.

# pass the summary data onto label_glue
ggplot(mydf) +
  geom_point(aes(x = x, y = y)) +
  facet_wrap(
    vars(red, blue),
    labeller = label_glue(
      "({.L}) Red = {red}, blue = {blue}\n(mean = {mean_y}, SD = {sd_y})",
      summary_data = mydf_stats))

facet_wrap labelled with two facet column values and two summary statistics, each numbered (a), (b), (c) and so on

Have fun! If you hit any snags, please feel free to file an issue here so that I can get on it! <3