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

在此处输入图像描述

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

在此处输入图像描述

非常感谢。接下来是数据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))
4

1 回答 1

4

你可以这样做,你可以调整+ 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 时,这两个图看起来不正确。

例子

于 2021-12-03T17:57:53.227 回答