r - 如何在网格中放置未定义数量的ggplot项目

标签 r list ggplot2 gridextra

<分区>

我有几个图表想放入 grid.arrange 函数中。但是,我并不总是事先知道网格中必须有多少图表,并且希望避免必须始终如一地检查代码来更改每个 grid.arrange 函数。

gg1 <- ggplot(mtcars, aes(cyl)) + geom_bar()
gg2 <- ggplot(mpg, aes(class, hwy)) + geom_bar(stat = "identity")
gg3 <- ggplot(mpg, aes(hwy)) + geom_area(stat = "bin")              

grid.test <- grid.arrange(gg1,gg2,gg3)

我已经尝试粘贴项目了。

grid.arrange(paste("gg", 1:3, sep= ""))

我还尝试将它们放入列表并对其进行解析,但无法在 grid.arrange 中获取未定义数量的它们。特别是 grid.arrange 仅在您抓取元素时才接受它,这不允许进行多项选择。

ggtest <- list(gg1,gg2,gg3)

grid.test <- grid.arrange(ggtest[[1:3]])

返回下标错误

ggtest <- list(gg1,gg2,gg3)

grid.test <- grid.arrange(ggtest[1:3])

仅返回“gList”错误中允许的“grobs”。

欢迎在这里提供任何帮助,也许我看错了,或者是否应该使用其他功能?

最佳答案

library(gridExtra)
library(ggplot2)

gg1 <- ggplot(mtcars, aes(cyl)) + geom_bar()
gg2 <- ggplot(mpg, aes(class, hwy)) + geom_bar(stat = "identity")
gg3 <- ggplot(mpg, aes(hwy)) + geom_area(stat = "bin")              

ggtest <- mget(paste0("gg", 1:3))

do.call(grid.arrange, ggtest)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

关于r - 如何在网格中放置未定义数量的ggplot项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959706/

相关文章:

c# - 将 List<String> 转换为 List<int>

r - 具有透明背景但字体可见的ggrepel标签

r - 向使用ggplot创建的密度图添加摘要信息

java - 检查列表列表是否包含列表中的任何子列表匹配元素

python - Python 列表操作的性能问题

r - 如何使用 ggplot 包从数据框的两列中绘制两条线并缩放 y 轴

r - 使用 Purrr::map2 循环列名称的两个向量,以便有条件地将多个列重新编码为新变量

r - 从数据框中子集特定行和最后一行

r - 如何使用plotly R创建带有颜色渐变的加权散点图/气泡图

r - 访问 opencpu session 中的对象