grid.arrange ggplot2按列而不是按行使用列表的图
问题描述:
我想要使用grid.arrange
从列表中创建ggplot2
图的多图,但在按行排列之前按列排列图。grid.arrange ggplot2按列而不是按行使用列表的图
gg_list1 <- list(qplot(mpg, disp, data = mtcars),
qplot(hp, wt, data = mtcars),
qplot(qsec, wt, data = mtcars))
gg_list2 <- list(qplot(mpg, disp, data = mtcars),
qplot(hp, wt, data = mtcars),
qplot(qsec, wt, data = mtcars))
我知道我能做到这一点:
do.call(grid.arrange,c(gg_list1,gg_list2 , ncol = 2, nrow = 3))
但它从上到下前左到右罢了。
我已经试过这样:
do.call(grid.arrange, c(gg_list1, arrangeGrob(gg_list2, nrow = 3), ncol = 2))
,但得到Error: length(widths) == ncol is not TRUE
任何想法?
答
可以使用grobs
参数来传递一个列表和as.table
参数,以填补逐列,因此与c
夷为平地,所有你需要的是
grid.arrange(grobs = c(gg_list1, gg_list2), ncol = 2, as.table = FALSE)
如果你想要一个更复杂的布局,使用layout_matrix
参数:
my_layout <- rbind(c(1, 1:3, 4), c(1, 1:3, 4), c(1, 1:3, 5), c(1, 1:3, 6))
my_layout
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 1 2 3 4
## [2,] 1 1 2 3 4
## [3,] 1 1 2 3 5
## [4,] 1 1 2 3 6
grid.arrange(grobs = c(gg_list1, gg_list2), layout_matrix = my_layout)
有关详细信息,请参见the arrangeGrob
vignette。
谢谢Alistaire。你的第一个使用'grob ='参数的例子在从上到下仍然是从左到右填充,fyi,但也许你知道吗?你的第二个例子工作!非常感谢你 - 我一直试图在过去的4小时里弄清楚这一点,不是开玩笑。 – Bonono
糟糕。您可以添加'as.table = FALSE'来按列填充;我会编辑。 – alistaire