使用拼接时如何减少图之间的空间

问题描述 投票:0回答:1

大家好,我正在使用一个小数据框在

ggplot2
中构建一些绘图。我的数据框是
df
,我将其作为
dput()
添加到最后。我有情节,当我使用
patchwork
时,问题就出现了。我希望最终的图没有空格,以便中间的线(即轴)可以将图连接在一起。这是代码:

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
的结果:

enter image description here

可以看出,由于中线和第二个图之间有空格,因此图未完全连接。我怎样才能删除那个空格?

enter image description here

非常感谢。接下来是数据

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))
r ggplot2 patchwork
1个回答
20
投票

您可以调整

+ 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时,这两个图看起来不正确。

example

© www.soinside.com 2019 - 2024. All rights reserved.