如何在grid.arrange中动态创建图? [英] How to create plots dynamically in grid.arrange in R?

查看:142
本文介绍了如何在grid.arrange中动态创建图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 get.single.plot ,它需要一个字符参数并返回一个 ggplot2 plot对象。我想建立一个 grid.arrange 对象,其中 n n 是(提到)字符参数的向量的大小。

I have a function get.single.plot, which takes one character argument and returns a ggplot2 plot object. I would like to build a grid.arrange object with n plots on it, where the n is the size of vector of (mentioned) character arguments.

例如,我喜欢这样的工作:

E.g., I woould like something like this to work:

character.argument.vector <- c("12", "1", "2")
grid.arrange(unlist(lapply(character.argument.vector, function(n) get.single.plot(n))),
         ncol = 1)

这样的东西不行 - 我收到以下信息:

Such thing does not work - I receive the following information:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main,  : 
input must be grobs!

我应该怎么做?

推荐答案

使用gridExtra v> = 2.0,你可以只需将格列(或图)列表格式转换为grid.arrange,

With gridExtra v>=2.0 you can simply pass a list of grobs (or plots) to grid.arrange,

grid.arrange(grobs = lapply(...), ncol=1)

这篇关于如何在grid.arrange中动态创建图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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