更改刻面标签尺寸的外观

Ale*_*der 13 r ggplot2 facet-wrap

我知道问题在这里被问到:有没有办法增加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))
Run Code Online (Sandbox Code Playgroud)

在我的情况下,似乎lineheight即使更改为也不会影响任何内容5.为什么?
如何使条形尺寸稍小但保持文字大小相同?

在此输入图像描述

在@Sandy Muspratt回答后编辑

如果只有一行,我们可以减小条带尺寸facets.

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

grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然而,在我的真实数据中,我有很多行如下图,当我改变g $高度的元素时,没有发生任何事情!

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

 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
Run Code Online (Sandbox Code Playgroud)

然后我试图改变1,7 and 11元素

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

grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

小平面标签大小没有变化.

> 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  
Run Code Online (Sandbox Code Playgroud)

San*_*att 15

使用边距

从大约ggplot2 ver 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")))
Run Code Online (Sandbox Code Playgroud)



原始答案更新为ggplot2 v2.2.0

你的facet_grid图表

这将减少条带的高度(如果需要,可以一直到零高度).需要为一个条带和三个凹槽设置高度.这将适用于您的特定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)
Run Code Online (Sandbox Code Playgroud)

你的Facet_wrap图表

页面上有三个条带.因此,有三个条带高度需要改变,三个树脂高度需要改变.

以下内容适用于您的特定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)
Run Code Online (Sandbox Code Playgroud)

如何找到相关的高度和凹凸?

g$heights返回高度向量.1null高度是情节面板.条带高度是前一个 - 即6,11,16.

g$layout返回一个数据框,其中包含最后一列中的grob的名称.需要改变高度的凹陷是名称以"条带"开头的凹凸.它们排在第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)
Run Code Online (Sandbox Code Playgroud)

每行多个面板

即使标题和图例位于顶部,也可以使用几乎相同的代码.计算有变化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)
Run Code Online (Sandbox Code Playgroud)

  • 我在答案中添加了一些代码.但我不能重现你的错误.首先,您使用的是R和ggplot2的最新版本吗?然后,从最简单的情节开始.另外,你对`pos`和`grobs`有什么价值?他们是否同意你从'g $ heights`和`g $ layout`得到的东西? (2认同)