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

回眸只為那壹抹淺笑 提交于 2019-12-23 10:44:46

问题


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

Expected result (ggplot2_1.0.1)

Question

What in middle earth is going on here?


回答1:


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)

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



来源:https://stackoverflow.com/questions/35043190/changing-height-of-strip-text-background-in-ggplot2-does-not-work-as-expected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!