'ggplot2, arrange multiple plots, all the same size, no gaps in between
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).
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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | S.T. |