如何在一个方面标签中包含希腊符号? [英] How can I have a Greek symbol within one of my facet labels?
问题描述
我正在尝试使用ggplot2(v.2_2.2.1)facet_wrap
创建图,并且我只需要在一个构面标签中使用希腊符号(五分之一).我尝试使用此处在堆栈溢出中发布的代码:
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:
- R Greek letters and normal letters for facet label in R facet grid
- How to add expressions to labels in facet_wrap?
- How can I use grid to edit a ggplot2 object to add math expressions to facet labels?
但没有成功(getGrob无法正常工作,对于mf_labeller也是如此).
but without any success (getGrob is not working, and that's true also for the 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轴刻度线,但是我无法在构面标签中执行此操作.
I'm able to rename the x-axis tick, but I just can't do that within the facet labels.
推荐答案
以下是使用labeller=label_parsed
的技巧.
首先,为使用解析的表达式的df$genes
定义标签:
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,"+"))))
然后在facet_wrap
中使用labeller=label_parsed
:
ggplot(df,aes(x=genes, y=value)) + geom_boxplot()+
facet_wrap(~genes, ncol = 5, scales = "free_x", labeller = label_parsed)
您可以看到它与x轴标签混淆了,但是您可以在scale_x_discrete
中按如下所示对其进行修复:
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,"+"))))
这篇关于如何在一个方面标签中包含希腊符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!