From ggplot2's reference manual, of label_both
:
library(ggplot2)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma"))
p <- ggplot(mtcars, aes(wt, mpg)) geom_point()
# Displaying both the values and the variables
p facet_grid(. ~ cyl, labeller = label_both)
This will give us facet strips with both variable name and its values. However, say if I want to change the appearance of the variable name, but not change the values, e.g.:
expression(italic(cyl))
So that I can get a cyl: 4 instead of cyl: 4, is it possible to do it using the labeller function?
CodePudding user response:
After a look at the docs and the source code label_both
has no parse
option. But similar to the approach in the answer referenced by @jared_mamrot in his comment you could use as_labeller
to create a custom labeller for which you use label_parsed
.
library(ggplot2)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("alpha", "beta", "gamma"))
p <- ggplot(mtcars, aes(wt, mpg))
geom_point()
p facet_grid(. ~ cyl,
labeller = as_labeller(
~ paste0("italic(cyl):", .x), label_parsed
)
)