【问题标题】:Changing the Appearance of Facet Labels size更改分面标签大小的外观
【发布时间】: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


【解决方案1】:

使用边距

从大约 ggplot2 版本 2.1.0 开始:在 theme 中,在 strip_text 元素中指定边距(参见 here)。

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 图表

这将降低条带的高度(如果需要,可以一直降低到零高度)。需要为一个带和三个 grob 设置高度。这将适用于您的特定 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)

如何找到相关的高度和格子?

g$heights 返回一个高度向量。 1null 高度是绘图面板。条带高度是之前的 1 - 即 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)

【讨论】:

  • @Alexander,原始解决方案适用于 facet_grid 示例。 facet-wrap 需要不同的方法。查看带有 facet_wrap 标题的编辑。
  • 好的,问题可能在于连续多个面板。前面的示例每行只有一个面板。我调查一下,但也许明天。但是,你能发布你的命令吗?
  • 我在答案中添加了一些代码。但我无法重现您的错误。首先,您使用的是最新版本的 R 和 ggplot2 吗?然后,从最简单的情节开始。另外,posgrobs 的值是多少?他们是否同意您从g$heightsg$layout 获得的信息?
  • 工作顺利。非常感谢。非常感谢它。周末愉快。
  • 我没有。不管传说的位置,也不管有没有传说,我都明白剧情。您是否使用答案中给出的代码和数据框?你得到ggplot吗?得到ggplot grob后,可以用grid.draw画出grow吗?你从pos 得到什么?应用第一组高度变化后,g$heights 会得到什么? grobs 能得到什么?应用第二组更改后,g$grobs[[13]]$heights 会得到什么?
猜你喜欢
  • 2019-11-24
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 2011-10-08
  • 2014-10-28
  • 2011-12-01
  • 2015-10-27
  • 2011-07-18
相关资源
最近更新 更多