我正在尝试使用ggplot2(v.2_2.2.1)facet_wrap
创建一个绘图,我需要在一个facet标签中有一个希腊符号(五个中)。我试图在stackoverflow上使用这里发布的代码:
example 1,example 2,example 3
但没有任何成功(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轴刻度,但我不能在刻面标签中这样做。对不起,如果很明显,我会变得疯狂...... 谢谢你的建议和帮助!
答案 0 :(得分:3)
这是一个使用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,"+"))))