rggplot2ggpubr

Reducing space between plots with ggarrange


I have an issue with large spaces between my plots using ggarrange. I know there is a solution to reduce empty space using grid.arrange, but I have been using ggarrange as it has nice options (aka "align" and "labels" as used here).

Other answers using ggarrange and grid.arrange have suggested changing the plot margin, (which I have tried to do here in this example), but this does not seem to fix my issue. I assume because even though I have blanked out the x axis, there is empty space between the plots where the "blank" axis is still taking up space.

Here is my example:

library(ggplot2)
library(ggpubr)

x<-seq(1,10)
y<-seq(10,1)

DF<-data.frame(x,y)

p1<- ggplot(DF, aes(x=x,y=y)) + 
  labs(x="Xlab", y="Ylab") +
  geom_line()+
  theme_bw()
  
  
p1TopNoX<-p1 + theme(axis.title.x=element_blank(),
                     axis.text.x=element_blank(),
                     axis.ticks.x=element_blank(),
                     plot.margin = margin(1, 1, 0, 0, "cm"))


p1MidNoX<-p1 + theme(axis.title.x=element_blank(),
                     axis.text.x=element_blank(),
                     axis.ticks.x=element_blank(),
                     plot.margin = margin(1, 1, 0, 0, "cm"))

p1BotX<-p1 + theme(plot.margin = margin(1, 1, 1, 0, "cm"))

ggarrange(p1TopNoX, p1MidNoX, p1BotX,
          nrow=3, labels = c("A","B", "C"), align= 'hv', 
          hjust=-4.5, vjust=3, font.label=list(color="black",size=25))

My goal is to have the bottom plot display the x axis, with the two plots above it having no axis labels (AKA a shared axis plot). Note the blank spaces between the plots. I would like to reduce space between these plots so that the three plots are almost touching.


Solution

  • You have two separate issues.

    1. The top margin is specified as 1cm
    2. The align argument in ggarange is affecting the margins

    If you set the "top" margin to 0, you'll get a lot less white space between the 3 plots (left panel). If you also remove the "h" in align='hv', you'll get your desired result (right panel).

    enter image description here


    GG1 <- ggarrange(p1TopNoX, p1MidNoX, p1BotX,
              nrow=3, labels = c("A","B", "C"), align='hv',
              hjust=-4.5, vjust=3, font.label=list(color="black",size=25))
    
    GG2 <- ggarrange(p1TopNoX, p1MidNoX, p1BotX, align='v',
              nrow=3, labels = c("A","B","C"), heights=c(2,2,3),
              hjust=-4.5, vjust=3, font.label=list(color="black",size=25))
    
    ggarrange(GG1, GG2, nrow=1)