我想使用本文中提供的指南用标题(“ID”,如下所示)标记我的构面网格,但我不明白如何摆弄 Grob 参数以使代码适应我的特定案例(不是因为缺乏努力)。我想在分面网格的顶部放置一个辅助标签,但如您所见,该标签并未覆盖 X 轴的整个范围。有人可以向我解释如何相应地调整此代码吗?我想了解 grob 参数实际上指的是什么,这样我就可以自己调整代码而不是复制/粘贴。非常感谢。
The code I used:
p<-ggplot(data=ex)+
geom_linerange(data=subset(ex11, detect=="fronts"), aes(x=value, ymin=start,ymax=end, color=hist,linetype="Active fronts"))+
geom_point(data=subset(ex11, detect=="rings"), aes(x=value, y=day, color=hist))+
coord_flip() +
labs(color="")+
labs(linetype="")+
labs(size="")+
facet_wrap(~ zid)
require(gtable)
# get gtable object
z <- ggplot_gtable(ggplot_build(p))
# add label for top strip
z <- gtable_add_rows(z, z$heights[[3]], 2)
z <- gtable_add_grob(z,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
textGrob("ID", 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)
我的数据的一个子集:
structure(list(zid = c(53L, 53L, 53L, 53L, 53L, 53L, 0L, 1L,
5L, 7L, 8L, 9L), hist = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L), .Label = c("NPB", "PB"), class = "factor"),
value = c(333, 333, 748, 514, 412, 355, 268.125, 299.25,
336.625, 255.69, 225.44, 229.31), day = c(268L, 269L, 270L,
271L, 272L, 273L, NA, NA, NA, NA, NA, NA), detect = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("fronts",
"rings"), class = "factor"), start = c(NA, NA, NA, NA, NA,
NA, 238L, 254L, 262L, 254L, 254L, 254L), end = c(NA, NA,
NA, NA, NA, NA, 246L, 262L, 270L, 270L, 270L, 270L)), .Names = c("zid",
"hist", "value", "day", "detect", "start", "end"), class = "data.frame", row.names = c(NA,
-12L))