在ggplot2中将facet标签更改为数学公式 [英] Changing facet label to math formula in ggplot2
问题描述
我想知道如何将 facet
标签更改为 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)
例如,我想将标记标签从 D
更改为 Y [1]
,其中1是下标。感谢您的帮助。
我发现这个答案但它对我不起作用。我使用 R 2.15.1
和 ggplot2 0.9.1
。
也许有人在某个时候改变了编辑Grob函数的名字。 (编辑:大约8个月前,它已被@hadley删除。)没有 geditGrob
但只是 editGrob
from pkg:grid似乎有效:
d < - ggplot(钻石,aes(克拉,价格,fill = ..density ..))+
xlim(0,2)+ stat_binhex(na .rm = TRUE)+ opts(aspect.ratio = 1)
#注意:ggplot2函数的变化会导致它从一开始就失败。
#Frank Harrell今年的答案显示`facet_warp`现在接受`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)
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)
For example, I want to change facet label from D
to Y[1]
, where 1 is subscript. Thanks in advance for your help.
I found this answer but it does not work for me. I'm using R 2.15.1
and ggplot2 0.9.1
.
Perhaps somebody has changed the name of the edit-Grob function at some point. (Edit: 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中将facet标签更改为数学公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!