4

I want to put black borders with labels and titles around each facet in facet_wrap. Something similar to this:

enter image description here

Sample data:

library(tidyverse)

mtcars %>% 
  mutate(gear = factor(gear, levels = c(4, 3, 5))) %>%
  ggplot(aes(mpg, disp)) + 
  geom_point() + 
  geom_smooth(method = "lm") + 
  facet_wrap(~am + gear)
tjebo
  • 21,977
  • 7
  • 58
  • 94
DJV
  • 4,743
  • 3
  • 19
  • 34
  • maybe helpful https://stackoverflow.com/questions/28652284/how-to-change-color-of-facet-borders-when-using-facet-grid and https://stackoverflow.com/questions/46220242/ggplot2-outside-panel-border-when-using-facet/46236791 – tjebo Dec 01 '19 at 14:49
  • just to clarify - you want the border not around each facet, but around each column of ? – tjebo Dec 01 '19 at 14:49
  • Thank you for the links, I'm looking into them. Yes, I wouldn't like around each facet per-se but around each column in this case. – DJV Dec 01 '19 at 14:51
  • I actually wouldn't really mind if it will be just around the columns, as I can arrange the facets order to match what ever order I want. – DJV Dec 01 '19 at 14:53
  • I think what you want to achieve is not easy without some hacking. I wonder if you may not want to overthink the aesthetic of your plot - maybe simply draw borders around the entire plot? You could plot your facets in several distinct plots and easily add a border and title [like in this question](https://stackoverflow.com/questions/52175766/draw-border-around-certain-rows-using-cowplot-and-ggplot2) – tjebo Dec 01 '19 at 18:17
  • @Tjebo I was thinking about it as my "plan B". I was hoping maybe someone will have a better/easier solution :) – DJV Dec 01 '19 at 21:46
  • How are you trying to split up gears? – camille Dec 04 '19 at 18:35
  • I'm not sure that I understood the question, but under `facet_wrap()`. Next I just want to border each column – DJV Dec 04 '19 at 21:13
  • 1
    @Tjebo - Thank you for the bounty! – DJV Dec 04 '19 at 21:14
  • With pleasure - I learned from it too :) I guess one could consider accepting the answer? There seems to be not much activity here otherwise.. – tjebo Dec 06 '19 at 15:06
  • I accept the answer :) – DJV Dec 07 '19 at 18:49

1 Answers1

2

You can do this by manually adding to the ggplot gtable:

library(tidyverse)    
library(grid)
library(gtable)

p <- mtcars %>% 
  mutate(gear = factor(gear, levels = c(4, 3, 5))) %>%
  ggplot(aes(mpg, disp)) + 
  geom_point() + 
  geom_smooth(method = "lm") + 
  facet_wrap(~am + gear)

g <- ggplotGrob(p)

# add basic black rectangle round the columns
gt <- gtable_add_grob(g, 
                     gList(
                      rectGrob(gp = gpar(col = "black", lwd=3, fill=NA)), 
                      rectGrob(gp = gpar(col = "black", lwd=3, fill=NA))),
                     t=7, b=13, l=c(5, 9))
# look
# grid.newpage(); grid.draw(gt)

# add title above columns
tit1 <-  textGrob("title1", x=0, hjust=0) ; tit2 =  textGrob("title2", x=0, hjust=0) 
gt <- gtable_add_rows(gt, grobHeight(tit1) + unit(0.5, "line"), 0)
gt <- gtable_add_grob(gt, gList(tit1, tit2), t=1, l=c(5, 9))
# draw
grid.newpage(); grid.draw(gt)

enter image description here

DJV
  • 4,743
  • 3
  • 19
  • 34
user20650
  • 24,654
  • 5
  • 56
  • 91
  • 2
    thanks @Tjebo; I picked up wee bit from [Baptiste's](https://stackoverflow.com/users/471093/baptiste) answers, and he provides a quick guide [here](https://cran.r-project.org/web/packages/gridExtra/vignettes/gtable.html) – user20650 Dec 04 '19 at 10:48
  • There may be another way now with this cool new package : https://www.stat.auckland.ac.nz/~paul/Reports/gggrid/gggrid.html – user20650 Oct 14 '21 at 23:49