【问题标题】:grid.arrange ggplot2 plots by columns instead of by row using listsgrid.arrange ggplot2 使用列表按列而不是按行绘图
【发布时间】:2017-04-14 03:04:36
【问题描述】:

我想使用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

有什么想法吗?

【问题讨论】:

    标签: r list ggplot2 gridextra r-grid


    【解决方案1】:

    您可以使用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

    【讨论】:

    • 谢谢阿利斯泰尔。您使用grob = 参数的第一个示例仍然从左到右填充,然后从上到下填充,仅供参考,但也许您知道?你的第二个例子成功了!非常感谢 - 在过去的 4 小时里我一直在努力解决这个问题,不是开玩笑。
    • 糟糕。您可以添加as.table = FALSE 以按列填充;我会编辑。
    猜你喜欢
    • 2015-09-08
    • 2011-05-08
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    相关资源
    最近更新 更多