Home > OS >  Auto-numbering panels in a plot
Auto-numbering panels in a plot

Time:09-17

I am creating a plot using ggplot::facet_wrap() and I wonder how to automatically add numbers/letters to the label/title of each panel?

gridExtra and complot solutions are not what I am looking for. It is more related to this SO post: enter image description here

CodePudding user response:

in case you want to use stickylabeller, you still can by using the github version.

devtools::install_github("rensa/stickylabeller")
library(ggplot2)

ggplot(mtcars, aes(qsec, mpg))   
  geom_point()   
  facet_wrap(. ~ am, labeller = stickylabeller::label_glue('({.n}) am = {am}'))  
  theme_bw(base_size = 12) 

enter image description here

CodePudding user response:

Start by defining a look-up of the labels, and then call in the facet_wrap() statement as here:

library(ggplot2)

levels <- sort(unique(mtcars$am))
labels <- c(glue::glue("({LETTERS[levels 1]}) am = {levels}", collapse = ""))
names(labels) <- levels

ggplot(mtcars, aes(qsec, mpg))   
  geom_point()   
  facet_wrap(. ~ am, labeller = labeller(am = labels))  
  theme_bw(base_size = 12)

enter image description here

  • Related