更改Facet标签大小的外观

16

我知道这个问题已经在这里提出了:有没有办法增加面板中strip.text栏的高度?

我想减小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,似乎也不会影响任何内容。为什么?
如何使条形图尺寸稍微变小,但保持文本大小不变?

enter image description here

@Sandy Muspratt回答后编辑

如果只有一行facets,我们可以减小条形图的大小。

g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm")  # Set the height

grid.newpage()
grid.draw(g)

enter image description here

但是,在我的真实数据中,我有许多类似下面的绘图行,并且当我更改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))

enter image description here

 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和11元素

g$heights[c(3,7,11)] = unit(.4, "cm")  # Set the height

grid.newpage()
grid.draw(g)

enter image description here

该标签大小没有变化。

> 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 窗口时,那个白色空间变得非常重要。如果我可以控制它的大小,每个窗口的绘图区域就会增加。这就是为什么! - Alexander
1
如您在问题中发布的链接所建议的那样,需要稍微修改您的分面变量,在每个字符串的开头和结尾添加 "\n"。具体操作为:cabbage_exp$Date <- paste0("\n", cabbage_exp$Date, "\n") - inscaven
@inscaven 是的,我之前没有意识到。谢谢! - Alexander
1个回答

24

使用边距

从ggplot2版本2.1.0开始:在theme中,可以在strip_text元素中指定边距(参见此处)。

library(ggplot2)
library(gcookbook) # For the data set

p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))

  p +
  theme(strip.text.x = element_text(margin = margin(.1, 0, .1, 0, "cm")))

更新至 ggplot2 v2.2.0 的原始答案

您的 facet_grid 图表

这将减小条带的高度(如果您愿意,甚至可以将其减小到零高度)。需要为一个条带和三个 grobs 设置高度。这将适用于您特定的 facet_grid 示例。

library(ggplot2)
library(grid)
library(gtable)
library(gcookbook) # For the data set

p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(. ~ Date) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))

g = ggplotGrob(p)

g$heights[6] = unit(0.4, "cm")  # Set the height

for(i in 13:15) g$grobs[[i]]$heights = unit(1, "npc") # Set height of grobs

grid.newpage()
grid.draw(g)

您的Facet_wrap图表

页面上有三条带子。因此,需要更改三个带子的高度,以及需要更改三个grob的高度。

以下内容适用于您特定的facet_wrap示例。

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)

for(i in c(6,11,16)) g$heights[[i]] = unit(0.4,"cm")   # Three strip heights changed
for(i in c(17,18,19)) g$grobs[[i]]$heights <-  unit(1, "npc")   # The height of three grobs changed

grid.newpage()
grid.draw(g)

如何找到相关的高度和grobs? g$heights 返回一个高度向量。其中,1null高度是绘图面板的高度。条形高度为其前一项-即6、11、16。 g$layout 返回一个带有最后一列中grobs名称的数据框。需要更改其高度的grobs的名称以"strip"开头。它们位于第17、18、19行。
稍加概括:
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)

# The heights that need changing are in positions one less than the plot panels
pos =  c(subset(g$layout, grepl("panel", g$layout$name), select = t))
for(i in pos) g$heights[i-1] = unit(0.4,"cm")

# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc")      
grid.newpage()
grid.draw(g)

一行多个面板

几乎相同的代码可以使用,即使有一个标题和图例位于顶部。在计算pos时有所改变,但即使没有这种改变,代码也可以运行。

library(ggplot2)
library(grid)

# Some data
df = data.frame(x= rnorm(100), y = rnorm(100), z = sample(1:12, 100, T), col = sample(c("a","b"), 100, T))

# The plot
p = ggplot(df, aes(x = x, y = y, colour = col)) +
   geom_point() +
   labs(title = "Made-up data") + 
   facet_wrap(~ z, nrow = 4) +
   theme(legend.position = "top")

g = ggplotGrob(p)

# The heights that need changing are in positions one less than the plot panels
pos =  c(unique(subset(g$layout, grepl("panel", g$layout$name), select = t)))
for(i in pos) g$heights[i-1] = unit(0.2, "cm")

# The grobs that need their heights changed:
grobs = which(grepl("strip", g$layout$name))
for(i in grobs) g$grobs[[i]]$heights <-  unit(1, "npc") 

grid.newpage()
grid.draw(g)

1
@Alexander,原始解决方案适用于facet_grid示例。 facet-wrap需要不同的方法。请参见带有facet_wrap标题的编辑内容。 - Sandy Muspratt
1
好的,可能问题是由于多个面板在一行中。之前的例子每行只有一个面板。我会研究一下,但也许明天再看。不过,您能否发布您的命令? - Sandy Muspratt
2
我已经在答案中添加了一些代码,但是我无法重现您的错误。首先,请确保您正在使用R和ggplot2的最新版本。然后,从最简单的图开始。此外,您得到的posgrobs值是什么?它们是否与您从g$heightsg$layout中获得的值相符? - Sandy Muspratt
1
我不在乎图例的位置,或者是否有图例,我只关心绘图。您是否按照答案中给出的代码和数据框使用?您能得到ggplot吗?在获取ggplot grob之后,您可以使用grid.draw绘制grow吗?您从pos中得到了什么?在应用第一组高度更改后,您对g$heights得到了什么?您对grobs得到了什么?在应用第二组更改后,您对g$grobs[[13]]$heights得到了什么? - Sandy Muspratt
1
你会注意到高度为4、8、12和16的值是“0.2”。它们应该是“0.2厘米”。这表明您的网格版本已经过时。您使用的是哪个版本的R?如果不是3.3.0,则更新R,将同时更新网格。 - Sandy Muspratt
显示剩余12条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接