5
###Load libraries

library(ggplot2)
library(gtable)

###Build plot

d <- ggplot(mtcars, aes(x=gear)) + 
            geom_bar(aes(y=gear), stat="identity", position="dodge") +
            facet_wrap(~cyl)

###Change height of strip text

g <- ggplotGrob(d)
g$heights[[3]] = unit(2,"in")
grid.newpage()
grid.draw(g)

Obtained result (ggplot2_2.0.0)

enter image description here

Expected result (ggplot2_1.0.1)

enter image description here

Question

What in middle earth is going on here?

Roland
  • 127,288
  • 10
  • 191
  • 288
shekeine
  • 1,445
  • 10
  • 22
  • @Axeman I did. Diagram 2, under the heading "Expected result". – shekeine Jan 27 '16 at 20:31
  • @konvas, As stated in the question, the problem is on Mac, on Linux everything works fine. `sessionInfo` on mac correctly reports `ggplot2_2.0.0`, same for Linux. Running the sample code in Linux yields the expected result, but not on Mac. Rstudio has been exonerated as well. Thanks. – shekeine Feb 08 '16 at 15:18
  • @Konvas Thanks for pointing that out, after digging and tinkering with various ggplot2 versions, I have found that this only happens in `ggplot2_2.0.0`. The Issue is OS independent too (whew) i.e. versions before `ggplot2_2.0.0` give the expected result on Os X. I have edited the question accordingly. – shekeine Feb 08 '16 at 20:14
  • +one but for what it's worth, I like the obtained result better than the expected result – C8H10N4O2 Feb 08 '16 at 20:58
  • @C8H10N4O2 No, you should not like the expected result. If you had multiple ggplots with facet titles of varying lengths angled at say, 45 degrees (hence different strip.text heights) and then arrangeGrob-ed them you would see why ;-) – shekeine Feb 08 '16 at 22:22

1 Answers1

6

This seems to do the trick

g <- ggplotGrob(d)
g$heights[[3]] = unit(2,"in")
g$grobs[[5]]$heights <- g$grobs[[6]]$heights <-
    g$grobs[[7]]$heights <- unit(1, "native") # or "npc"
grid.newpage()
grid.draw(g)

enter image description here

It also works if you replace unit(1, "native") by a positive number, or TRUE (I am not sure why though - probably at some point this is coerced to a default type unit, likely "npc")

konvas
  • 14,126
  • 2
  • 40
  • 46
  • does the trick.. but why after assigning new units to g$heights does it only taks the value but not the units – user20650 Feb 09 '16 at 20:52
  • Plus one for the trick, but I need the height in cm: very handy in my use case. – shekeine Feb 10 '16 at 09:37
  • @Shekeine Which height do you need in cm? You can definitely change units to "cm" in `g$heights[[3]] <- unit(2,"in")` - then using `unit(1, "native")` for the heights of the grobs will just make them occupy the whole space - does this make sense? If you want to specify them manually you can also use `g$grobs[[5]]$heights <- .. <- unit(1, "cm")` but then they may extend beyond the area you want – konvas Feb 10 '16 at 11:11
  • @konvas Yes yes, I meant, I need to specify the heights of the strip text backgrounds in absolute units (whether `cm` or `in`), just not npc. The above issue persists irrespective of whether `cm` or `in` are used. Doing `g$grobs[[5]]$heights <- unit(2, cm)` extends the strip text background downward over the plot's panel i.e, "beyond the area I want". I didn't entirely understand your suggestion with using `unit(1, "native")` "for the heights of the grobs", could you post a working example? – shekeine Feb 10 '16 at 12:29
  • @Shekeine I am not expert in the grid package but my understanding is that if you do `g$heights[[3]] = unit(X, Y)` (using whatever number X and unit Y) and then `g$grobs[[5]]$heights <- .. <- unit(1, "native")` then the strips will have height X and the whole background will be grey. (by heights of the grobs I mean `g$grobs[[i]]$heights`) I am not sure how this differs from what you want, perhaps I have not understood correctly what you are trying to achieve – konvas Feb 10 '16 at 12:56
  • @konvas many thanks, that's clearer and I just tried this out again (properly this time :-). Indeed, it works. I wonder if it is "best practice" though (compared to the old way that works with older `ggplot2` versions.. – shekeine Feb 10 '16 at 17:29
  • @konvas Hello there, eeerrr, I think we need a solution for ggplot V2.2.0, seems this does not work anymore. I wonder if I should post a new question or "reopen" this one... – shekeine Feb 05 '17 at 14:50
  • Does this answer help? http://stackoverflow.com/questions/36783189/changing-the-appearance-of-facet-labels-size – konvas Feb 05 '17 at 19:19