作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 ggplot2
中的分面图上添加分面标题作为 strip .我的 MWE 抛出错误。
library(ggplot2)
library(gtable)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
# get gtable object
Plot1 <- ggplot_gtable(ggplot_build(p))
# add label for top strip
Plot1 <- gtable_add_rows(Plot1, Plot1$heights[[3]], 2)
Plot1 <- gtable_add_grob(Plot1,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
textGrob("Cyl", gp = gpar(col = gray(1)))),
3, 4, 3, 10, name = paste(runif(2)))
# add margins
Plot1 <- gtable_add_rows(Plot1, unit(1/8, "line"), 2)
# draw it
grid.newpage()
print(grid.draw(Plot1))
最佳答案
编辑 更新到 ggplot 版本 3.0.0
我认为你很接近。您在 add grob 命令中输入了错误的行号和列号。您的新 strip 跨越第 5 到第 9 列;即在 add grob 命令中将 10 更改为 9。
library(ggplot2)
library(gtable)
library(grid)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(. ~ cyl)
z <- ggplotGrob(p)
# New strip at the top
z <- gtable_add_rows(z, z$height[7], pos = 6) # New row added below row 6
# Check the layout
gtable_show_layout(z) # New strip goes into row 7
# New strip spans columns 5 to 9
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = "gray85", size = .5)),
textGrob("Number of Cylinders", gp = gpar(cex = .75, fontface = 'bold', col = "black"))),
t=7, l=5, b=7, r=9, name = c("a", "b"))
# Add small gap between strips - below row 6
z <- gtable_add_rows(z, unit(2/10, "line"), 7)
# Draw it
grid.newpage()
grid.draw(z)
library(ggplot2)
library(gtable)
library(grid)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p <- p + facet_grid(cyl ~ .)
z <- ggplotGrob(p)
# New strip to the right
z <- gtable_add_cols(z, z$widths[6], pos = 6) # New column added to the right of column 6
# Check the layout
gtable_show_layout(z) # New strip goes into column 7
# New strip spans rows 7 to 11
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = "gray85", size = .5)),
textGrob("Number of Cylinders", rot = -90,
gp = gpar(cex = .75, fontface = 'bold', col = "black"))),
t=7, l=7, b=11, r=7, name = c("a", "b"))
# Add small gap between strips - to the right of column 6
z <- gtable_add_cols(z, unit(2/10, "line"), 6)
# Draw it
grid.newpage()
grid.draw(z)
关于r - 将分面标题注释为分面上的 strip ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22818061/
我是一名优秀的程序员,十分优秀!