2

I would like to arrange multiple plots into one figure, without any gaps between the plot area, and all plots being exactly the same size (see image below for a sketch of my desired figure, which comprises 6 individual plots).

desired figure, composed of 6 individual plots

I have tried ggarrange (ggpubr) and plot_grid (cowplot) and a couple others but they seem to have the same problem - you can align the plots to get them the same size, but not arrange them closer to each other.

ggdraw in the cowplot package allows one to specify exactly where the plots go, but they are all slightly different sizes.

Is there a way to overcome this?

Basically, I want to make the plot area (where the data is displayed, NOT the axes and labels) the same for six graphs, then arrange them contiguously. So far I have not found anyway to do this.

The x-axis variable is the same for all 6 graphs, but the y is different, and there are multiple series on some of the graphs but not others, so I cannot use the facet option in ggplot2.

Please help!

EDIT: Sorry, some more info - because my plots have different y variables, the plot margins and the plot panels are all slightly different sizes. So that means when I try to add them all together, they are out of alignment. Plots different sizes out of alignment. plot_grid from cowplot allows you to specify position, and size, BUT, the size is the size of the whole plot, panel and margins included. Because of the Y-axis labels, the margins are different sizes, and resizing them using the arguments to plot_grid is not so simple.

S.T.
  • 81
  • 1
  • 5
  • Have you seen these questions? Unsure if they're duplicates, but they might help: [reduce space between grid.arrange plots](https://stackoverflow.com/questions/13299496/reduce-space-between-grid-arrange-plots), and [Side-by-side plots with ggplot2](https://stackoverflow.com/q/1249548/8366499) – divibisan Mar 13 '19 at 21:38
  • `cowplot::draw_plot` lets you specify position, height and width. – pogibas Mar 13 '19 at 21:38
  • 1
    I use **library(gridExtra)** to combine plots - works perfectly. Specify **layout <- rbind(c(1, 1, 1), c(2, 2, 2))** and use **grid.arrange(p1, p2, layout_matrix = layout)** AND for p1 and p2 (two ggplots) add **theme(plot.margin=unit(c(0,0,0,0),"cm"))** to control space between two plots – AnnaZ Mar 13 '19 at 21:47
  • Possible duplicate of [Side-by-side plots with ggplot2](https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2) – divibisan Mar 14 '19 at 19:26
  • Thank you for your responses. None of the options were quite what I was looking for: https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2 still leaves some gaps between the plots, and I need the gaps gone completely. This one https://stackoverflow.com/questions/13299496/reduce-space-between-grid-arrange-plots I don't even understand how to use, being relatively new to R. – S.T. Mar 15 '19 at 01:09
  • @Annaz that seems to only work if the plot panels are already the same size, otherwise it results in a painstaking process of a multitude of axis adjustments trying to get the panels the same size. – S.T. Mar 15 '19 at 01:12

1 Answers1

3

So I managed to find an answer, based on some code from here: https://community.rstudio.com/t/how-do-i-control-the-size-of-the-panel-in-a-ggplot-so-they-are-consistent/14377/8

The plots first need to be passed through the align_plots() function in cowplot() , to make a list of the plots:

`multiplot <- align_plots(plotAA,plotBB,plotCC,plotDD,plotEE,plotFF, align = "hv")`    

Then the panels of the graphs will be all the same size, and they can be arranged using ggdraw() and draw_plot() as follows:

`ggdraw() + draw_grob(multiplot[[1]], 0,0.565,.6,.4) +
  draw_grob(multiplot[[2]], 0.396,0.565,0.6,0.4) + 
  draw_grob(multiplot[[3]], 0, 0.285, 0.6,0.4) +
  draw_grob(multiplot[[4]], 0.396, 0.285, 0.6, 0.4) +
  draw_grob(multiplot[[5]], 0,0.004, 0.6, 0.4) +
  draw_grob(multiplot[[6]], 0.396,0.004,0.6,0.4)`

to give the final figure: Desired plot arrangement

I hope this helps somebody else in the future, Thanks.

S.T.
  • 81
  • 1
  • 5
  • EDIT: that should be the 'draw_plot()' function in place of 'draw_grob()' (though either works). – S.T. Mar 15 '19 at 01:38
  • 2nd Edit: the first thing I would do is make the panel and plot backgrounds transparent using `theme(panel.background = element_rect(colour=NA, fill = "transparent"), plot.background = element_rect(colour=NA, fill = "transparent"))`. Then it is easier to position the graphs correctly. – S.T. Mar 15 '19 at 02:30