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:
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)
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)