如何在我的多面标签之一中包含希腊符号?

问题描述 投票:0回答:1

我正在尝试使用 ggplot2 (v. 2_2.2.1)

facet_wrap
创建一个绘图,并且我只需要在一个分面标签中包含一个希腊符号(共五个)。我尝试使用 Stack Overflow 上发布的代码:

但没有任何成功(getGrob 不起作用,对于 mf_labeller 也是如此)。

有人可以帮我吗?

这是示例代码:

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,"+"))))

我可以重命名 x 轴刻度,但我无法在构面标签中执行此操作。

r ggplot2 label facet-wrap
1个回答
7
投票

这是一个使用

labeller=label_parsed
的技巧。

首先,为使用要解析的表达式的

df$genes
定义标签:

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

然后在

labeller=label_parsed
中使用
facet_wrap

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

正如您所看到的,这与 x 轴标签混淆,但您可以在

scale_x_discrete
中修复它,如下所示:

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

编辑:为了回应发布的后续问题,这里是使用

label_parsed
表示多个变量的解决方案。在变量字符串中包含请求的空间。

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)

固定 x 轴:

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

© www.soinside.com 2019 - 2024. All rights reserved.