rplotggpubr

ggarrange plot seemingly overlap


I am trying to put 4 plots on same page from a number of different R bioinformatics packages using ggarrange (but none of the plots are ggplot made).

But I have a problem that my plots seem to overlap each other and I only see 4th plot (one on bottom right) fully.

I can reproduce this situation by the following command:

plot(1:10)
p1 <- recordPlot()
dev.off()

plot(1:10)
p2 <- recordPlot()
dev.off()

plot(1:10)
p3 <- recordPlot()
dev.off()

plot(1:10)
p4 <- recordPlot()
dev.off()

pdf(paste0("test",".pdf"), width = 20, height = 10)
ggarrange(plotlist = list(print(p1), print(p2), print(p3), print(p4)),
          ncol = 2, nrow = 2, align = "hv")
dev.off()

Resulting plot in the pdf file

I tried reducing the margins with par(mar=c()) but that didn't help.

I would be grateful for any suggestion to fix this overlap.

Thanks,

Dani


Solution

  • Each plot has a solid white background. Make it transparent if you want to stop the background of each plot from partially overlapping the foreground of the previously drawn plot (though personally I would also use patchwork here)

    par(bg = "#00000000")
    plot(1:10)
    p1 <- recordPlot()
    dev.off()
    
    par(bg = "#00000000")
    plot(1:10)
    p2 <- recordPlot()
    dev.off()
    
    par(bg = "#00000000")
    plot(1:10)
    p3 <- recordPlot()
    dev.off()
    
    par(bg = "#00000000")
    plot(1:10)
    p4 <- recordPlot()
    dev.off()
    
    ggpubr::ggarrange(plotlist = list(print(p1), print(p2), print(p3), print(p4)),
              ncol = 2, nrow = 2, align = "hv")
    

    enter image description here