使用r中的选项进行do.call排列ggplot列表 [英] do.call with options in r to arrange ggplot list

查看:68
本文介绍了使用r中的选项进行do.call排列ggplot列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的虚拟数据为:

x=1:7
y=1:7
df = data.frame(x=x,y=y)
bp <- vector("list", length = 4)
for (i in 1:4) {
 bp[[i]] <- ggplot(df,aes(x,y))+geom_point()
}

我的ggplot对象放在一个名为 bp 的列表中,通过它我可以生成一个四图网格,如下所示:

I have my ggplot objects in a list called bp with which I can generate a four-plot-grid as:

figure <- ggarrange(bp[[1]], bp[[2]], bp[[3]], bp[[4]],
                    labels = c("A", "B", "C", "D"),
                    ncol = 2, nrow = 2) 

现在,我不想键入 bp [[1]] .. bp [[4]] ,并且不想使用

Now, I don't want to type bp[[1]]..bp[[4]] and want to use something like

do.call(ggarrange,bp)就像使用函数 grid.arrange 一样,例如 do.call(grid.arrange,bp). do.call(grid.arrange,bp)不显示面板标签,因此我想使用 ggarrange 并仍然希望传递这些参数:

do.call(ggarrange, bp) as you would do with function grid.arrange like do.call(grid.arrange, bp). do.call(grid.arrange, bp) doesn't show the panel labels so I would like to use ggarrange and still would want to pass these arguments:

labels = c("A", "B", "C", "D"),
                    ncol = 2, nrow = 2

在这种情况下如何使用 do.call ?有更好的方法吗?

How can I use do.call in this case? Is there a better way to do this?

推荐答案

选项是将其放置在命名的 list 中,并连接 list 元素

The option is to place it in a named list and concatenate the list elements

library(ggpubr)
do.call(ggarrange, c(bp, list(labels = c("A", "B", "C", "D"),
                 ncol = 2, nrow = 2) ))

在这里.OP的要求是将 do.call ggarrange 一起使用.当然,无需 do.call

Here, the. OP's request is about using do.call with ggarrange. Of course, it can be done without do.call

这篇关于使用r中的选项进行do.call排列ggplot列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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