组小平面标签的标题

时间:2015-09-14 01:29:59

标签: r ggplot2 facet gtable grob

我想使用this帖子中提供的指南标注我的facet网格标题(“ID”,如下所示),但我不明白如何摆弄Grob参数使代码适应我的特定情况(不是因为缺乏努力)。我想在小平面网格的顶部放置一个辅助标签,但正如您所看到的,标签未覆盖X轴的整个范围。有人可以向我解释如何相应调整此代码吗?我想了解grob参数实际上是指什么,所以我可以自己调整代码而不是复制/粘贴。非常感谢。

enter image description here

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))

0 个答案:

没有答案