5

I am quite new to ggplot2 (and R) so bear with me. Using facet_wrap, I have created a multi-panel plot. I want to label them with alphabetical letters (starting with the top-left panel labelled with (A), and the next panel with (b) and so on.) but I am not sure how to go about doing this. I want this to be located outside of the plot (ideally at the top left of each panel).

I came across grid.text and ggtitle, but I am not sure how I would implement these to get the desired outcome.

I came across this post and with the given answers it seems possible to manually label each figure, but I would like the labelling to be done automatically, in order.

I would deeply appreciate any advice/guides.

Community
  • 1
  • 1
Joshlhj
  • 67
  • 1
  • 6

1 Answers1

5
p <- qplot(displ, hwy, data = mpg) + facet_wrap(~ cyl) + 
    theme(panel.margin=unit(1,"line"))

library(gtable)
g <- ggplotGrob(p)
strips <- g$layout[grep("strip_t", g$layout$name), ]
titles <- lapply(paste0("(", letters[seq_len(nrow(strips))], ")"), 
                 textGrob, x = 0, hjust = 0, vjust = 1)
g <- gtable_add_grob(g, grobs = titles, 
                     t = strips$t, b = strips$b - 2, 
                     l = strips$l, r = strips$r)
grid.newpage()
grid.draw(g)

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thank you so much for your answer! The 'grob' is so new to me so I had to get around to learn more about it. One question though. How to I fine-tune the position of the labels? I would like something in between "strips$b-1" and "strips$b-2". Thanks you! – Joshlhj May 11 '15 at 01:24
  • you probably want to play with vjust in textGrob, between 0 and 1 – baptiste May 11 '15 at 01:47
  • Thanks. Because my original intention was to make the labels and the plots feel less crammed, I tried widening the strip size and it looks better (in my opinion). But I will play around with textGrob too. Thanks again! – Joshlhj May 11 '15 at 02:11