将方面标签更改为 ggplot2 中的数学公式 [英] Changing facet label to math formula in ggplot2
问题描述
我想知道如何将 facet
标签更改为 ggplot2
中的数学公式.
I wonder how to change the facet
label to math formula in ggplot2
.
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d + facet_wrap(~ color, ncol = 4)
例如,我想将 facet 标签从 D
更改为 Y[1]
,其中 1 是下标.预先感谢您的帮助.
For example, I want to change facet label from D
to Y[1]
, where 1 is subscript. Thanks in advance for your help.
我找到了这个答案,但对我不起作用.我正在使用 R 2.15.1
和 ggplot2 0.9.1
.
I found this answer but it does not work for me. I'm using R 2.15.1
and ggplot2 0.9.1
.
推荐答案
也许有人在某个时候更改了 edit-Grob 函数的名称.(它在大约 8 个月前被 @hadley 删除.)geditGrob
但只是来自 pkg:grid 的 editGrob
似乎工作:
Perhaps somebody has changed the name of the edit-Grob function at some point. ( It was removed by @hadley about 8 months ago.) There is no geditGrob
but just editGrob
from pkg:grid seems to work:
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
#Note: changes in ggplot2 functions cause this to fail from the very beginning now.
# Frank Harrell's answer this year suggests `facet_warp` now accepts `labeller`
d <- d + facet_wrap(~ color, ncol = 4)
grob <- ggplotGrob(d)
strip_elem <- grid.ls(getGrob(grob, "strip.text.x", grep=TRUE, global=TRUE))$name
#strip.text.x.text.1535
#strip.text.x.text.1541
#strip.text.x.text.1547
#strip.text.x.text.1553
#strip.text.x.text.1559
#strip.text.x.text.1565
#strip.text.x.text.1571
grob <- editGrob(grob, strip_elem[1], label=expression(Y[1]))
grid.draw(grob)
这篇关于将方面标签更改为 ggplot2 中的数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!