调整 strip.background 的大小以匹配 ggplot facet_wrap 中的 strip.text

标签 r plot ggplot2 facet

我正在尝试制作许多“小倍数”图。

(旁白/背景)我有太多方面无法在单个图中显示,因此我需要手动将它们划分为单独的图。我希望我能通过想要的 ncolnrowfacet_wrap它会根据我的需要制作多少。事实并非如此。

因为我有很多方面,所以空间非常宝贵。所以,我想减少刻面标签的大小。很容易减少 strip.text字体大小,但遗憾的是 strip.background rectangle 不与字体大小相同,也不提供大小参数(除了边框大小)。将其设置为 element_blank()实际上并没有删除为其预留的空间,它只是使相同的空间变得透明。

MWE:

set.seed(1)
xcat = rep(1:10, 1000)
yvalue1 = rep(runif(10,5,7),1000)
yvalue2 = rep(runif(10,4,6),1000)
id = rep(1:1000,each=10)

df = data.frame(id,xcat,yvalue1,yvalue2)

for(i in seq(1, length(unique(df$id)), by=100)){
  print(
    ggplot(subset(df, id %in% unique(df$id)[i:(i+99)]), aes(x=xcat)) +
      annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + 
      annotate("line", y=yvalue2, x=xcat, colour="grey20") + 
      theme(axis.text.x = element_text(size=5),
            axis.text.y = element_text(size=5),
            strip.text = element_text(size=5)) +
      facet_wrap(~id, ncol=10, nrow=10) + 
      expand_limits(y=0))
}

example image with default grey strip.background

关于如何降低该 strip.background 的高度以使其不占用太多空间的任何想法?理想情况下,它的大小应与 id 文本的确切高度一致。

作为奖励,如何减少刻面之间的间距? (我缺少 facet_wrap 中的设置吗?)

使用 facet_wrap 在 RStudio 中渲染也需要很长时间(没有 facet_wrap 它要快得多。

最佳答案

您想要的结果是可以通过修改 grobs 来实现的创建者 ggplot .这个过程有些困惑,必然需要一些手动微调:

抛开对问题没有影响的循环,我们可以从减少刻面之间的间距开始。这很容易通过添加 panel.spacing 来完成。 theme 的论据.

library(ggplot2)

d <- ggplot(subset(df, id %in% unique(df$id)[1:(100)]), aes(x=xcat)) +
  annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + 
  annotate("line", y=yvalue2, x=xcat, colour="grey20") + 
  theme(axis.text.x = element_text(size=5),
        axis.text.y = element_text(size=5),
        strip.text = element_text(size=5),
        panel.spacing = unit(.5, 'pt')) +
  facet_wrap(~id, ncol=10, nrow=10) +
  expand_limits(y=0)

然后我们必须生成一个 ggplot2 plot grob。这将创建组成绘图的单个元素的列表。
g <- ggplotGrob(d)

head(g, 5)
# TableGrob (5 x 43) "layout": 13 grobs
#    z         cells        name                                    grob
# 1  3 ( 5- 5, 4- 4)  axis-t-1-1                          zeroGrob[NULL]
# 2  3 ( 5- 5, 8- 8)  axis-t-2-1                          zeroGrob[NULL]
# 3  3 ( 5- 5,12-12)  axis-t-3-1                          zeroGrob[NULL]
# 4  3 ( 5- 5,16-16)  axis-t-4-1                          zeroGrob[NULL]
# 5  3 ( 5- 5,20-20)  axis-t-5-1                          zeroGrob[NULL]
# 6  3 ( 5- 5,24-24)  axis-t-6-1                          zeroGrob[NULL]
# 7  3 ( 5- 5,28-28)  axis-t-7-1                          zeroGrob[NULL]
# 8  3 ( 5- 5,32-32)  axis-t-8-1                          zeroGrob[NULL]
# 9  3 ( 5- 5,36-36)  axis-t-9-1                          zeroGrob[NULL]
# 10 3 ( 5- 5,40-40) axis-t-10-1                          zeroGrob[NULL]
# 11 4 ( 4- 4, 4-40)      xlab-t                          zeroGrob[NULL]
# 12 8 ( 3- 3, 4-40)    subtitle zeroGrob[plot.subtitle..zeroGrob.77669]
# 13 9 ( 2- 2, 4-40)       title    zeroGrob[plot.title..zeroGrob.77668]

我们的目标是找到并固定相对于 strip 的高度。
for(i in 1:length(g$grobs)) {
  if(g$grobs[[i]]$name == 'strip') g$grobs[[i]]$heights <- unit(5, 'points')
}

不幸的是strip grobs也受不同heights的影响主要情节被分成,所以我们也必须减少那些。我还没有找到一种方法来以编程方式识别哪些高度指的是 strip ,但正在检查 g$heights找出我们需要的很快。
g$heights
# [1] 5.5pt               0cm                 0cm                 0cm                 0cm                
# [6] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [11] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [16] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [21] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [26] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [31] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [36] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [41] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [46] 0.518897450532725cm 1null               0cm                 0.5pt               0cm                
# [51] 0.518897450532725cm 1null               0.306264269406393cm 1grobheight         0cm                
# [56] 5.5pt       

strip_grob_position <- seq(6, length(g$heights) - 5, 5)
for(i in strip_grob_position) {
  g$heights[[i]] = unit(.05, 'cm')
}

我们最终可以得出结果:
library(grid)
grid.newpage()
grid.draw(g)

enter image description here

我根本不是这方面的专家,所以我可能说或解释了错误或不准确的事情。此外,可能有更好(更简单、更像 ggplot)的方法来获得结果。

关于调整 strip.background 的大小以匹配 ggplot facet_wrap 中的 strip.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45086847/

相关文章:

html - R:将 ggplot 对象转换为交互式图表

r - 有没有一种有效的方法来计算 R 中 sf 中多边形的所有成对交集(不使用 for 循环)?

r - R 中变量的上标

r - 如何在 R 中绘制具有 3 个因子的响应变量?

r - 使用ggrepel对齐标签

python - 堆叠条形图的边缘被切断

r - R中函数调用的优先级

r - 使用 ggmap 绘制 OpenStreetMap

r - 没有使用 RMySQL 选择数据库

用于 Linux 的 C++ 2D 绘图工具