控制杂物添加到拼凑而成的填充 [英] Control padding of grobs added to patchwork

查看:61
本文介绍了控制杂物添加到拼凑而成的填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是(v1.0.0)创建于2021-04-09 sup>

解决方案

据我了解,问题不在 Patchwork 一方.查看图例的 gtable 的布局,我们看到它由5行和5列组成,并且图例将放置在中间的单元格中:

 <代码> p_legend<-cowplot :: get_legend(p1)p_legend#>TableGrob(5 x 5)指南盒":2个grobs#>z单元名称#>99_a788e923bf245af3853cee162f5f8bc9 1(3-3,3-3)指南#>0(2-4,2-4)图例.box.background#>格罗布#>99_a788e923bf245af3853cee162f5f8bc9 gtable [layout]#>zeroGrob [NULL]gtable :: gtable_show_layout(p_legend) 

因此,当添加图例 patchwork 中心时,是 gtable 布局所要求的.

控制图例的位置或填充的一种方法是通过 cowplot :: gtable_squash_cols 压缩第一列,如果需要,可以通过添加所需数量的新列来添加一些填充通过 gtable :: gtable_add_cols :

填充

 <代码>#压入第一列p_legend<-cowplot :: gtable_squash_cols(p_legend,1)#通过添加新的col添加一些填充p_legend<-gtable :: gtable_add_cols(p_legend,unit(.1,"cm"),pos = 1)p_main<-p1<-ggplot(数据=数据,映射= aes(x =县,y =百分比,填充=变量))+geom_col(show.legend = FALSE)+scale_fill_manual(值= c(#CF232B",#942192",#000000"))p_main + plot_spacer()+ p_legend +plot_layout(widths = c(12.5,1.5,4))&主题(plot.margin = margin(),plot.background = element_rect(colour ="black")) 

This is a follow up problem to this question. The OP asked for a way to arrange parts of a plot in specific distances. I think teunbrand gave a very good answer.

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.

Having shorter labels reveals the problem - when adding a grob, patchwork centres this grob, basically padding equally to all sides.

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.

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)

Created on 2021-04-09 by the reprex package (v1.0.0)

解决方案

As far as I get it the issue is not on patchworks side. 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:

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.

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:

# 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"))

这篇关于控制杂物添加到拼凑而成的填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆