ggplot2中更改条形文字背景的高度不能按预期工作 [英] Changing height of strip text background in ggplot2 does not work as expected

查看:206
本文介绍了ggplot2中更改条形文字背景的高度不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ###加载库

库(ggplot2)
库(gtable)

###构建图

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

$ bg < - ggplotGrob(d)
g $ heights [[3]] b

###更改条形文字的高度=单元(2,in)
grid.newpage()
grid.draw(g)



获得结果( ggplot2_2.0.0





预期结果( ggplot2_1.0.1





问题



地球在这里?

解决方案

这似乎是个窍门

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

p>

如果用正数替换 unit(1,native) TRUE (我不确定为什么 - 可能在某些时候这被强制为一个默认的单位,可能是npc)


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

解决方案

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

这篇关于ggplot2中更改条形文字背景的高度不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆