Search code examples
rggplot2cowplot

Keeping plot spacing intact using Cowplot align


I am trying to make some 'close stacked' plots using Cowplot. I can get the effect I want using the following MWE:

library(ggplot2)
library(cowplot)

disp_plot <- ggplot(data=mtcars,aes(x= hp,y=disp)) + geom_point()+ 
             theme(plot.margin = unit(c(0.3,0.3,0.0,0.15), "cm"),
             axis.text.x =element_blank(),axis.title.x =element_blank())
mpg_plot <- ggplot(data=mtcars,aes(x= hp,y=mpg)) + geom_point()+
            theme(plot.margin = unit(c(0.0,0.3,0.0,0.15), "cm"),
            axis.text.x =element_blank(),axis.title.x =element_blank())
qsec_plot <- ggplot(data=mtcars,aes(x= hp,y=qsec)) + geom_point()+
             theme(plot.margin = unit(c(0.0,0.3,0.3,0.15), "cm"))
mp <- plot_grid(disp_plot, mpg_plot, qsec_plot,ncol = 1,align='v', axis = 'l')

correct spacing

Notice that mpg is not the same vertical scale as the others. To get the plots to be the same vertical size as well, I'd like to use "align='hv'" in plot_grid. However, this causes the spacing set by the margins to revert and the figures fly apart again. Is there a way I can preserve my spacing? incorrect spacing


Solution

  • Not cowplot, but this seems to work.

    library(egg)
    library(grid)
    grid.draw(ggarrange(plots=list(disp_plot, mpg_plot, qsec_plot)))
    

    enter image description here