Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing height of strip text background in ggplot2 does not work as expected

Tags:

r

ggplot2

gtable

###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?

like image 384
shekeine Avatar asked Jan 27 '16 16:01

shekeine


1 Answers

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")

like image 169
konvas Avatar answered Sep 26 '22 17:09

konvas