作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我经常使用数值进行分面。我希望提供足够的信息来解释补充标题中的这些分面值,类似于轴标题。贴标机选项会重复许多不必要的文本,并且无法用于较长的变量标题。
有什么建议吗?
默认值:
test<-data.frame(x=1:20, y=21:40, facet.a=rep(c(1,2),10), facet.b=rep(c(1,2), each=20))
qplot(data=test, x=x, y=y, facets=facet.b~facet.a)
我想要什么:
我在 ggplot 中能做的最好的事情:
qplot(data=test, x=x, y=y)+facet_grid(facet.b~facet.a, labeller=label_both)
正如@Hendy 所指出的,类似于: add a secondary y axis to ggplot2 plots - make it perfect
最佳答案
由于最新的ggplot2
内部使用了gtable
,因此修改图形非常容易:
library(ggplot2)
test <- data.frame(x=1:20, y=21:40,
facet.a=rep(c(1,2),10),
facet.b=rep(c(1,2), each=20))
p <- qplot(data=test, x=x, y=y, facets=facet.b~facet.a)
# get gtable object
z <- ggplotGrob(p)
library(grid)
library(gtable)
# add label for right strip
z <- gtable_add_cols(z, unit(z$widths[[7]], 'cm'), 7)
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
textGrob("Variable 1", rot = -90, gp = gpar(col = gray(1)))),
4, 8, 6, name = paste(runif(2)))
# add label for top strip
z <- gtable_add_rows(z, unit(z$heights[[3]], 'cm'), 2)
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
textGrob("Variable 2", gp = gpar(col = gray(1)))),
3, 4, 3, 6, name = paste(runif(2)))
# add margins
z <- gtable_add_cols(z, unit(1/8, "line"), 7)
z <- gtable_add_rows(z, unit(1/8, "line"), 3)
# draw it
grid.newpage()
grid.draw(z)
当然,您可以编写一个自动添加 strip 标签的函数。 ggplot2 的 future 版本可能具有此功能;但不确定。
关于r - 如何在 ggplot2 中向构面添加通用标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11353287/
我是一名优秀的程序员,十分优秀!