在ggplot2的两行中排列三个大小相同的图 [英] Arrange three plots of the same size on two rows in ggplot2

查看:37
本文介绍了在ggplot2的两行中排列三个大小相同的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中排列三个大小相同(或多或少二次)的图.我希望在上排显示两个图,在下排显示一个图.下排中的单个图应居中.我只知道当下部图左对齐时如何排列图.

I'm trying to arrange three plots of the same size (more or less quadratic) in ggplot2. I want two plots to be in the upper row and one in the lower row. The single plot in the lower row should be centered. I only found out how to arrange the plots when the lower plot is left-aligned.

到目前为止,我一直在使用ggarrange来布置图(由于align ="v"命令).我的代码看起来像这样(首先我创建了三个图p1,p2,p3):

So far I've been using ggarrange to arrange plots (because of the align="v" command). My code looked something like that (first I created the three plots p1, p2,, p3):

ggsave("H:/Documents/graph.pdf", height=8, width=10, units="in", dpi=300,
   ggarrange(p1, p2, p3, ncol=2, nrow=2, align="v"))

我也尝试过将grid.arrange与layout_matrix命令一起使用,但这根本不起作用!

I've also tried using grid.arrange with the layout_matrix command but that didn't work at all!

有人知道我如何将下图居中吗?

Does anyone have an idea how I can center the lower plot?

推荐答案

layout_matrix 确实是您需要的:

p1 <- p2 <- p3 <- qplot(mpg, wt, data = mtcars)
grid.arrange(p1, p2, p3, layout_matrix = matrix(c(1, 3, 2, 3), nrow = 2))

其中

matrix(c(1, 3, 2, 3), nrow = 2)
#      [,1] [,2]
# [1,]    1    2
# [2,]    3    3

如果您是第三个图居中的意思,则显示哪个图占据了最终输出的哪一部分.

shows which plot occupies which part of the final output, if that's what you mean by the third plot being centered.

或者,

(layout_matrix <- matrix(c(1, 1, 2, 2, 4, 3, 3, 4), nrow = 2, byrow = TRUE))
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    2    2
# [2,]    4    3    3    4
grid.arrange(p1, p2, p3, layout_matrix = layout_matrix)

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

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