简体   繁体   English

添加到拼凑而成的 grobs 的控制填充

[英]Control padding of grobs added to patchwork

This is a follow up problem to this question .这是这个问题的后续问题 The OP asked for a way to arrange parts of a plot in specific distances. OP 要求一种将 plot 的部件以特定距离排列的方法。 I think teunbrand gave a very good answer.我认为 teunbrand 给出了一个很好的答案。

My own suggestion (extract the legend with cowplot, and stitch them to a plot in desired proportions) is not fully satisfactory, because it worked only "by chance" in the given example - the legend labels were long enough to center the legend grob into the viewport for the third plot.我自己的建议(用cowplot提取图例,并将它们按所需比例缝合到plot)并不完全令人满意,因为它在给定的示例中只是“偶然”起作用-图例标签足够长以将图例集中到第三个 plot 的视口。

Having shorter labels reveals the problem - when adding a grob, patchwork centres this grob, basically padding equally to all sides.较短的标签揭示了问题 - 添加 grob 时,拼凑此 grob 居中,基本上向所有侧面均等填充。

My question is, do you know of a way to control this padding behaviour?我的问题是,你知道控制这种填充行为的方法吗?

Cowplot (or any other ggplot combining package for that sake) also very welcome. Cowplot(或任何其他结合 package 的 ggplot )也非常受欢迎。

library(tidyverse)
library(patchwork)
data <- midwest %>% 
  head(5) %>% 
  select(2,23:25) %>%
  pivot_longer(cols=2:4,names_to="Variable", values_to="Percent") %>% 
  mutate(Variable=factor(Variable, 
                         levels=c("percbelowpoverty","percchildbelowpovert","percadultpoverty"),
                         labels = paste0("perc", 1:3)))

p1 <- 
  ggplot(data=data, mapping=aes(x=county, y=Percent, fill=Variable)) +
  geom_col() + 
  scale_fill_manual(values = c("#CF232B","#942192","#000000")) +
  theme(legend.background = element_rect(fill = "grey50"))

p_legend <- cowplot::get_legend(p1)

p_main <- p1 <- 
  ggplot(data=data, mapping=aes(x=county, y=Percent, fill=Variable)) +
  geom_col(show.legend = FALSE) + 
  scale_fill_manual(values = c("#CF232B","#942192","#000000"))

p_main + plot_spacer() + p_legend + 
  plot_layout(widths = c(12.5, 1.5, 4)) &
  theme(plot.margin = margin(),
        plot.background = element_rect(colour = "black"))

Not so desired result - the legend grob (with grey background) should be aligned to the left plot border (black line)不太理想的结果 - 图例 grob(灰色背景)应与左侧 plot 边框(黑线)对齐

Created on 2021-04-09 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 4 月 9 日创建

As far as I get it the issue is not on patchwork s side.据我所知,问题不在于patchwork Having a look at the layout of the legend's gtable we see that it is made up of 5 rows and 5 columns and that the legend is to be placed in the cell in the center:查看图例的gtable的布局,我们看到它由 5 行和 5 列组成,并且图例将放置在中心的单元格中:

p_legend <- cowplot::get_legend(p1)
p_legend
#> TableGrob (5 x 5) "guide-box": 2 grobs
#>                                     z     cells                  name
#> 99_a788e923bf245af3853cee162f5f8bc9 1 (3-3,3-3)                guides
#>                                     0 (2-4,2-4) legend.box.background
#>                                               grob
#> 99_a788e923bf245af3853cee162f5f8bc9 gtable[layout]
#>                                     zeroGrob[NULL]
gtable::gtable_show_layout(p_legend)

Hence, when adding the legend patchwork centers is as demanded by the gtable layout.因此,当添加图例patchwork中心时, gtable布局需要。

One option to control the positioning or the padding of the legend would be to squash the first column via cowplot::gtable_squash_cols and if desired add some padding by adding a new column with the desired amount of padding via gtable::gtable_add_cols :控制图例的定位或填充的一个选项是通过cowplot::gtable_squash_cols挤压第一列,如果需要,通过通过gtable::gtable_add_cols ::gtable_add_cols 添加具有所需填充量的新列来添加一些填充:

# Squash first column
p_legend <- cowplot::gtable_squash_cols(p_legend, 1)
# Add some padding by adding a new col
p_legend <- gtable::gtable_add_cols(p_legend, unit(.1, "cm"), pos = 1)

p_main <- p1 <- 
  ggplot(data=data, mapping=aes(x=county, y=Percent, fill=Variable)) +
  geom_col(show.legend = FALSE) + 
  scale_fill_manual(values = c("#CF232B","#942192","#000000"))

p_main + plot_spacer() + p_legend + 
  plot_layout(widths = c(12.5, 1.5, 4)) &
  theme(plot.margin = margin(),
        plot.background = element_rect(colour = "black"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM