grid.arrange ggplot2按列而不是按列使用列表进行绘图 [英] grid.arrange ggplot2 plots by columns instead of by row using lists
问题描述
我想使用 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))
但是得到错误:长度(宽度)== ncol不是TRUE
任何想法?
您可以使用 grobs
参数来传递一个列表和 as.table
参数来填充列的方式,所以用 c
,所有你需要的是
$ p $ lt; code> grid.arrange(grobs = c(gg_list1,gg_list2),ncol = 2,as.table = FALSE)
如果您想要更复杂的布局,请使用 layout_matrix
参数:
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)
请参阅 arrangeGrob
vignette 获取详细信息。
I want create a multiplot of ggplot2
plots from a list using grid.arrange
but arrange them by columns before doing it by rows.
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))
I know I can do this:
do.call(grid.arrange,c(gg_list1,gg_list2 , ncol = 2, nrow = 3))
but it fills from left to right before top to bottom.
I've tried this:
do.call(grid.arrange, c(gg_list1, arrangeGrob(gg_list2, nrow = 3), ncol = 2))
But get Error: length(widths) == ncol is not TRUE
Any ideas?
You can use the grobs
parameter to pass a list and the as.table
parameter to fill column-wise, so flattened with c
, all you need is
grid.arrange(grobs = c(gg_list1, gg_list2), ncol = 2, as.table = FALSE)
If you want a more complex layout, use the layout_matrix
parameter:
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)
See the arrangeGrob
vignette for details.
这篇关于grid.arrange ggplot2按列而不是按列使用列表进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!