0

I'd like to label my facet grid with a title ("ID", as seen below) using the guidelines provided in this post, but I don't understand how to fiddle with the Grob parameters enough to adapt the code to my particular case (not because of lack of effort). I would like to place a secondary label over the top of the facet grid, but as you can see, the label is not covering the entire extent of the X axis. Can someone kindly explain to me how to tweak this code accordingly? I'd like to understand what the grob parameters are actually referring to so I can tweak the code myself rather than copy/paste. Many thanks.

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)

A subset of my data:

    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))
Community
  • 1
  • 1
SilvanD
  • 325
  • 2
  • 14
  • 1
    We _kinda need_ `p` to help. – hrbrmstr Sep 14 '15 at 01:38
  • 1
    Your code is still not reproducible. But your problem lies with the four numbers in the `gtable_add_grob` command: 3, 4, 3, and 6. They represent the top, left, bottom, and right extents respectively of the grob being added to the layout. The 6 is the problem. Check the layout - `gtable_show_layout(z)`. – Sandy Muspratt Sep 14 '15 at 07:38
  • 1
    Also, check [here](http://stackoverflow.com/questions/22818061/annotating-facet-title-as-strip-over-facet/22825447#22825447) and [here](http://stackoverflow.com/questions/29311772/ggplot2-more-complex-faceting/29323739#29323739) for additional examples. – Sandy Muspratt Sep 14 '15 at 07:50

0 Answers0