Skip to content

Instantly share code, notes, and snippets.

@swo
Last active March 17, 2021 01:42
Show Gist options
  • Save swo/a7a91f509a193cdd3df225fbeec5b005 to your computer and use it in GitHub Desktop.
Save swo/a7a91f509a193cdd3df225fbeec5b005 to your computer and use it in GitHub Desktop.
Italics in ggplot facet labels
# 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)
@ginnolivia
Copy link

ginnolivia commented Mar 16, 2021

Hi. Is label1 supposed to be short_label? I was wondering if you could provide some context for the options in the tibble function.

I'm working on a wonderfully similar issue, trying to italicize parts of the facet labels. This seems to be a shortcoming in ggplot and I've tried just about everything.

My plot code is below and my facet labels consist of: shigella, ttr, campy, ybbw, and ec which I want to show up as "EIEC/_Shigella_spp.", "Salmonella spp.", "Campylobacter jejuni", and "Escherichia coli" for the last two. This is where I am having issues.

size_paths<-ggplot(dist_for_graph_path,aes(x=dist_path,y=concen_path,color=stages_path))+geom_point(size=3)+geom_line()+theme_bw()+
  facet_grid(. ~names_path, (I know this is where I input labeller=label_parsed)+
  ggtitle("a. Molecular detections")+
  theme(legend.position ="",axis.text=element_text(size=10),
        axis.title=element_text(size=12),legend.title=element_text(""),
        strip.text=element_text(size=12))+
  scale_y_log10(limits=c(.01,100000),breaks=c(0.1,1,10,100,1000,10000,100000),
                labels=c("1e-1","1e+0","1e+1","1e+2","1e+3","1e+4","1e+5")) + 
  annotation_logticks(sides="l")+
  xlab("") + ylab('Mean gc per m'^'3'~'air')+labs(fill="")+labs(color="")+ 
  scale_color_brewer(palette = "Dark2")+
  scale_x_continuous(breaks=c(1,10,20,40,60,80,100),limits=c(0,100))

Any input or insight/clarification would be appreciated!!

@swo
Copy link
Author

swo commented Mar 17, 2021

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.

library(tidyverse)

data <- tibble(
  species = c("shigella", "ttr"),
  label = c("'EIEC/' * italic('Shigella') ~ 'spp.'", "italic('Salmonella') ~ 'species'"),
  x = c(1, 2),
  y = c(3, 4)
)

data %>%
  ggplot(aes(x, y)) +
  facet_wrap(vars(label), labeller = label_parsed) +
  geom_point()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment