Last active
August 30, 2024 02:37
-
-
Save swo/a7a91f509a193cdd3df225fbeec5b005 to your computer and use it in GitHub Desktop.
Italics in ggplot facet labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Say I have some short-hand labels like "Ec/q", | |
# which actually stand for "E. coli & quinolones", | |
# but I want "E. coli" to be in italics | |
library(tidyverse) | |
n <- 100 | |
tibble( | |
x = rnorm(n), | |
y = rnorm(n), | |
short_label = sample(c("Ec/q", "Sp/m"), size = n, replace = TRUE), | |
long_label = recode( | |
short_label, | |
"Ec/q" = "italic('E. coli')~'& quinolones'", | |
"Sp/m" = "italic('S. pneumoniae')~'& quinolones'" | |
) | |
) %>% | |
ggplot(aes(x, y)) + | |
geom_point() + | |
facet_wrap(~ long_label, labeller = label_parsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm surprised you found this old gist of mine!
I fixed that typo you noted and updated the syntax for more recent tidyverse versions. See below for how I would deal with your case. The key is learning about R expressions. This thread has some examples of different ways to do expressions. I like writing them as strings and having ggplot do all the parsing.
Tibble is just a way to make a data frame.