ggplot2,排列多个图,所有图大小均相同,且两者之间没有间隙 [英] ggplot2, arrange multiple plots, all the same size, no gaps in between

查看:98
本文介绍了ggplot2,排列多个图,所有图大小均相同,且两者之间没有间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个地块排列成一个图形,在地块区域之间没有任何间隙,并且所有地块的大小都完全相同(请参见下图以获取我想要的图形的草图,其中包括6个单独的地块)./p>

我尝试了 ggarrange (ggpubr)和 plot_grid (cowplot)以及其他几个问题,但它们似乎都存在相同的问题-您可以对齐图以获取它们相同的大小,但不要将它们彼此靠得更近.

cowplot软件包中的

ggdraw 允许您确切指定绘图的位置,但是它们的大小略有不同.

有办法克服吗?

基本上,我想使六个图形的绘图区域(显示数据的地方,轴和标签)相同,然后连续排列它们.到目前为止,我还没有找到执行此操作的方法.

所有6张图的x轴变量都相同,但是y不同,并且在某些图上有多个系列,而在其他图上没有,所以我不能在ggplot2中使用facet选项.

请帮助!

对不起,更多信息-因为我的绘图具有不同的y变量,所以绘图边距和绘图面板的大小都略有不同.因此,这意味着当我尝试将它们全部添加在一起时,它们是不一致的.绘制不对齐的不同大小.cowplot中的plot_grid允许您指定位置和大小,但BUT的大小是整个图的大小,包括的面板和边距.由于有Y轴标签,因此页边距大小不同,使用plot_grid的参数调整它们的大小并不是那么简单.

解决方案

因此,我基于以下代码设法找到了答案:

然后,图形的面板将全部具有相同的大小,并且可以使用 ggdraw() draw_plot()如下:

 `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)` 

给出最终数字:所需的情节安排

我希望这对以后的人有所帮助,谢谢.

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.

解决方案

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.

这篇关于ggplot2,排列多个图,所有图大小均相同,且两者之间没有间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆