Home > Software design >  How to reduce the space between to plots when using patchwork
How to reduce the space between to plots when using patchwork

Time:12-04

Hi all I am working with a little dataframe to build some plots in ggplot2. My dataframe is df and I include it as dput() at the end. I have the plots and the problem rises when I use patchwork. I want the final plot without spaces so that the line in the middle, which is an axis, can join the plots together. Here is the code:

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')

This is the result for G3:

enter image description here

It can be seen that plots are not fully joined as there is an space between middle line and second plot. How can I remove that space?

enter image description here

Many thanks. The data df is next:

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

CodePudding user response:

You can do this you can adjust plot_layout(widths = c()) or you could adjust the margins using & theme(plot.margin = ...) however, I don't think plot.margin will work in this case.

To implement widths into your plot, you will need to add a spacer plot and use widths to adjust the spacer so that the plots full join together

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

Here the width of plot1 is 4, the width of the spacer plot is -1.1 which allows you to join the plots together and the width of plot2 is 4.5. I am not sure why plot2 needs to have a larger width than plot1 but the two plots don't look right when both their widths are set to 4.

example

  • Related