3

I'm trying to create a plot using ggplot2 (v. 2_2.2.1) facet_wrap, and I need to have a Greek symbol in only one facet label (out of five). I have tried to use code posted here on Stack Overflow:

but without any success (getGrob is not working, and that's true also for the mf_labeller).

Can anyone help me with that?

Here is the example code:

df <- data.frame(genes = rep(c("BA","MLL","pos","neg","PMLalpha+"),5), value = sample(1:100, 25, replace=TRUE))
df$genes <- factor(df$genes, levels = c("BA","MLL","pos","neg","PMLalpha+"), ordered = TRUE)
ggplot(df,aes(x=genes, y=value)) + geom_boxplot()+ 
  facet_wrap(~genes, ncol = 5,scales = "free_x")+
  scale_x_discrete(name="",
               breaks = c("BA","MLL","pos","neg","PMLalpha+"),
               labels = c("BA","MLL","pos","neg",expression(paste("PML", alpha,"+"))))

I'm able to rename the x-axis tick, but I just can't do that within the facet labels.

TylerH
  • 20,799
  • 66
  • 75
  • 101
LenkaH
  • 33
  • 1
  • 3

1 Answers1

7

Here is a trick that uses labeller=label_parsed.

First, define labels for df$genes that uses the expression to be parsed:

df$genes <- factor(df$genes, levels = c("BA","MLL","pos","neg","PMLalpha+"),
 ordered = TRUE, labels=c("BA","MLL","pos","neg",expression(paste("PML", alpha,"+"))))

Then use labeller=label_parsed in facet_wrap:

ggplot(df,aes(x=genes, y=value)) + geom_boxplot() + 
  facet_wrap(~genes, ncol = 5, scales = "free_x", labeller = label_parsed)

As you can see this messes with the x-axis labels, but you can fix it in scale_x_discrete as follows:

ggplot(df,aes(x=genes, y=value)) + geom_boxplot() + 
  facet_wrap(~genes, ncol = 5, scales = "free_x", labeller = label_parsed) +
  scale_x_discrete(name="",
               breaks = c("BA","MLL","pos","neg","paste(\"PML\", alpha, \"+\")"),
               labels = c("BA","MLL","pos","neg", expression(paste("PML", alpha,"+"))))

enter image description here

Edit: In response to follow-up question posted, here is the solution for using label_parsed for more than one variable. Includes requested space in the variable strings.

df$var2 <- c(rep(c('trt a','trt b'), NROW(df)/2),'trt a')
df$var2 <- factor(df$var2, levels=c('trt a','trt b'), ordered=T, labels=c(expression(paste("trt ", alpha)), expression(paste("trt ", beta))))

ggplot(df,aes(x=var2, y=value)) + geom_boxplot() + facet_wrap(~var2, ncol = 5, scales = "free_x", labeller = label_parsed)

Fix the x-axis:

ggplot(df,aes(x=var2, y=value)) + geom_boxplot() + 
  facet_wrap(~var2, ncol = 5, scales = "free_x", labeller = label_parsed) + 
  scale_x_discrete(name="",
                 breaks = c(expression(paste("trt ", alpha)), expression(paste("trt ", beta))),
                 labels = c(expression(paste("trt ", alpha)), expression(paste("trt ", beta))))

enter image description here

Djork
  • 3,319
  • 1
  • 16
  • 27
  • Any idea how to only use `label_parsed` for more than one variable? For example add another variable to `df` that is a character string with a space (the space is important). `df$var2 <- c(rep(c('trt a','trt b'),NROW(df)/2),'trt a')`. Now run the plot code and you get the error: `Error in parse(text = as.character(values)) : :1:5: unexpected symbol 1: trt a ^` because it's trying to evaluate this as a math expression. Any suggestions? – dandrews Jan 15 '22 at 15:21
  • 1
    Perhaps a way to ignore the variable with the space? – dandrews Jan 15 '22 at 15:23
  • Agree above, added solution to above question to the answer above. – Djork Mar 12 '22 at 06:05