将n个ggplots排列成较低的三角形矩阵形状 [英] Arrange n ggplots into lower triangle matrix shape

查看:229
本文介绍了将n个ggplots排列成较低的三角形矩阵形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有n个ggplot对象,它们总是有正确的数字来构成一个矩阵的下三角形(没有对角线)。我可以如何安排他们:

I have n ggplot objects that will always have the correct number to make a lower triangle of a matrix (no diagonals). How can I arrange them in this order:

1
2 3
4 5 6
7 8 9 10

形成一个网格(在这里n = 10)?

to form a grid (n = 10 here)?

下面是制作n个情节的数据,以及我希望它看起来如何我有n = 6。

Here is data to make n plots and how I'd like it to look of I had n = 6.

n <- sample(1:4, 1)
N <- sum(n:1)

library(ggplot2)
theplot <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
plots <- lapply(1:N, function(i) theplot)
plots <- mapply(function(x, y) x + ggtitle(y), plots, 
    paste("PLOT", seq_along(plots)), SIMPLIFY=FALSE)

我怀疑 gridExtra 在这里可能有用,但有空白的窗格。我打开基地或添加包的想法。

I suspect gridExtra may be useful here but there are blank panes. I'm open to base or add on package ideas.

推荐答案

您可以将矩阵布局传递给grid.arrange, p>

You can pass a matrix layout to grid.arrange,

library(ggplot2)
library(gridExtra)
plots <- lapply(1:10, function(id) ggplot() + ggtitle(id))

m <- matrix(NA, 4, 4)
m[lower.tri(m, diag = T)] <- 1:10
grid.arrange(grobs = plots, layout_matrix = m)

这篇关于将n个ggplots排列成较低的三角形矩阵形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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