0

I wonder that how can we place labels left or right side of the facet_grid strips.

As a reproducible example I would like to start with this example

library(ggplot2)
    ggplot(mtcars, aes("", hp)) + 
      geom_boxplot(width=0.7, position=position_dodge(0.7)) + 
      theme_bw() +
      facet_grid(. ~ vs + am + carb,switch = 'both',labeller = label_both) +
      theme(panel.spacing=unit(0.2,"lines"),
            strip.background=element_rect(color="grey30", fill="grey90"),
            panel.border=element_rect(color="grey90"),
            axis.ticks.x=element_blank())+
            #strip.placement="outside") +
      labs(x="")

enter image description here

The plot which I look for;

enter image description here

Alexander
  • 4,527
  • 5
  • 51
  • 98
  • 1
    Don't think can be done "directly". Try plotting as you have in the second graph, then call something like `grid.text("vs", x = 0.98 , y = 0.1)`, though you'll have to fiddle the numbers to get it exactly where you need it. – Scransom Aug 01 '17 at 00:11
  • 1
    E.g. https://stackoverflow.com/a/29594608/2706826 – Scransom Aug 01 '17 at 00:12

1 Answers1

3

you can always add the titles manually to the gtable,

enter image description here

g <- ggplotGrob(p)

library(gtable)
library(grid)
library(gridExtra)
pos <- subset(g$layout, grepl("strip",name))
titles <- tableGrob(c("vs","am","carb"), theme = ttheme_minimal(9))
titles$heights <- unit(rep(1,3), "null")
g$widths[ncol(g)] <- sum(titles$widths)
g <- gtable_add_grob(g, titles, t=unique(pos$t), l = ncol(g)) 
grid.newpage()
grid.draw(g)
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Thanks for the answer. Actually I was looking for this kind of answer as I am also trying to merge common strips into one common strip as in this [question](https://stackoverflow.com/questions/40732543/seeking-workaround-for-gtable-add-grob-code-broken-by-ggplot-2-2-0) . Is is possible to add these lines into the `OverlappingStripLabels` ? – Alexander Aug 01 '17 at 00:59
  • as `pos` will have two different definition. – Alexander Aug 01 '17 at 01:07
  • Hi @baptiste did you able to look up to my last comments? I have tried some but still unable merge your solution to `OverlappingStripLabels()` – Alexander Aug 01 '17 at 05:27