使用拼凑时如何减少地块之间的 space

How to reduce the space between to plots when using patchwork

大家好,我正在使用一个小数据框在 ggplot2 中构建一些绘图。我的数据框是 df,最后我将其作为 dput() 包含在内。我有情节,当我使用 patchwork 时问题就出现了。我想要没有 spaces 的最终图,以便中间的线(轴)可以将图连接在一起。这是代码:

library(ggplot2)
library(patchwork)
library(cowplot)
library(ggtext)
#Plot 1
G1 <- ggplot(df,aes(x=Var1,y=Var2))+
  geom_line(aes(color=Group,group=Group),size=1)+
  geom_point(aes(color=Group,group=Group,shape=Group),size=2)+
  scale_y_continuous(limits = c(0,NA),
                     sec.axis = dup_axis(name = '',breaks = NULL,labels = NULL))+
  theme_half_open(12) +
  background_grid() +
  theme(
    strip.background = element_blank(),
    strip.text = element_textbox(
      size = 12,
      face = 'bold',
      color = "white", fill = "#5D729D", box.color = "#4A618C",
      halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
      padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
    )
  )+
  theme(legend.position = 'top',
        axis.title = element_text(color='black',face='bold'),
        axis.text = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        panel.grid = element_blank(),
        legend.justification = 'center',
        plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(color='black',
                                  size=12,
                                  face='bold',hjust=0.5),
        plot.caption = element_text(face='bold'))
#Plot 2
G2 <- ggplot(df,aes(x=Var1,y=Var3))+
  geom_line(aes(color=Group,group=Group),size=1)+
  geom_point(aes(color=Group,group=Group,shape=Group),size=2)+
  scale_y_continuous(limits = c(0,NA),position = 'right')+
  theme_half_open(12) +
  background_grid() +
  theme(
    strip.background = element_blank(),
    strip.text = element_textbox(
      size = 12,
      face = 'bold',
      color = "white", fill = "#5D729D", box.color = "#4A618C",
      halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
      padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
    )
  )+
  theme(legend.position = 'top',
        axis.title = element_text(color='black',face='bold'),
        axis.text = element_text(color='black',face='bold'),
        legend.text = element_text(color='black',face='bold'),
        legend.title = element_text(color='black',face='bold'),
        panel.grid = element_blank(),
        legend.justification = 'center',
        plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(color='black',
                                  size=12,
                                  face='bold',hjust=0.5),
        plot.caption = element_text(face='bold'))
#Merge plots
G3 <- G1+G2+plot_layout(guides = 'collect')&theme(legend.position = 'top')

这是 G3 的结果:

可以看出地块没有完全连接,因为中间线和第二个地块之间有一个 space。我怎样才能删除 space?

非常感谢。接下来是数据df

#Data
df <- structure(list(Var1 = c("A", "B", "C", "A", "B", "C"), Group = c("G1", 
"G1", "G1", "G2", "G2", "G2"), Var2 = c(1L, 3L, 4L, 8L, 9L, 0L
), Var3 = c(9L, 8L, 4L, 5L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-6L))

您可以这样做,您可以调整 + plot_layout(widths = c()) 或者您可以使用 & theme(plot.margin = ...) 调整页边距,但是,我认为 plot.margin 在这种情况下不起作用。

要在您的绘图中实现 widths,您需要添加一个间隔图并使用 widths 调整间隔以使绘图完全连接在一起

 G3 <- G1 + plot_spacer() + G2 + plot_layout(widths = c(4, -1.1 ,4.5),guides = "collect")& theme(legend.position = "top")

此处 plot1 的宽度为 4,间隔图的宽度为 -1.1,允许您将图连接在一起,plot2 的宽度为 4.5。我不确定为什么 plot2 需要比 plot1 更大的宽度,但是当两个图的宽度都设置为 4 时,它们看起来不正确。