【发布时间】:2016-08-15 10:57:52
【问题描述】:
我知道有人在这里问过这个问题:Is there a way to increase the height of the strip.text bar in a facet?
我想在不改变文本大小的情况下降低 strip.text 栏的高度。在当前情况下,文本和条形条墙之间总是留有空间。
这是我迄今为止尝试过的,
library(gcookbook) # For the data set
library(ggplot2)
ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(.~ Date) +
theme(strip.text = element_text(face="bold", size=9,lineheight=5.0),
strip.background = element_rect(fill="lightblue", colour="black",
size=1))
在我的情况下,lineheight 似乎不会影响任何事情,即使更改为5。为什么?
如何使条形条的大小变小一些但保持文本大小相同?
在@Sandy Muspratt 回答后编辑
如果只有一行facets,我们可以减小条带大小。
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
但是,在我的真实数据中,我有很多行如下图,当我更改 g$heights 的元素时,什么也没发生!
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
g = ggplotGrob(p)
g$heights
# [1] 5.5pt 0cm 0.66882800608828cm #1null 0cm 0.193302891933029cm
# [7] 0.66882800608828cm 1null 0cm #0.193302891933029cm 0.66882800608828cm 1null
# [13] 0.456194824961948cm 0cm 1grobheight 5.5pt
然后我尝试更改 1,7 and 11 元素
g$heights[c(3,7,11)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
分面标签大小没有变化。
> g$heights
[1] 5.5pt 1grobheight
[3] sum(0.2cm, sum(0.15cm, 0.8128cm, 0cm, 0.15cm), 0.2cm)+0.2cm 0.2
[5] 1null 0cm
[7] 0.193302891933029cm 0.2
[9] 1null 0cm
[11] 0.193302891933029cm 0.2
[13] 1null 0cm
[15] 0.193302891933029cm 0.2
[17] 1null 0.456194824961948cm
[19] 0cm 1grobheight
[21] 5.5pt
【问题讨论】:
-
@hrbrmstr 因为当我有很多
facet_wrap的窗口时,空白变得非常重要。如果我可以控制它的大小,每个窗口的绘图区域都会增加。这就是为什么! -
正如您在问题中发布的链接的答案中所建议的那样,有必要修改一点您的构面变量,在每个字符串的开头和结尾添加
"\n"cabbage_exp$Date <- paste0("\n", cabbage_exp$Date, "\n") -
@inscaven 是的,但我无法意识到。谢谢!
标签: r ggplot2 facet-wrap