gpt4 book ai didi

r - 在页面上布置多个 ggplot 图

转载 作者:行者123 更新时间:2023-12-03 19:44:10 24 4
gpt4 key购买 nike

我在循环内生成一个 ggplot 对象列表,如下所示:

myPlots = list()
for(i in 1:length(maturities)){
myPlots[[i]] <- ggplot(deltaIR.df, aes(sample = deltaIR.df[,i])) +
stat_qq() + stat_qq_line() +
labs(title=maturities[i],
x = "Theoretical (Normal)",
y = "Empirical Distribution")
}

根据数据集的不同,myPlots 中可能有 4 到 10 个图。我现在想将它们分两行打印在一页上,并尝试了各种方法,取得了不同程度的成功。最有前途的方法是
library(ggpubr)
grid.arrange(myPlots[[1]], myPlots[[2]], myPlots[[3]], myPlots[[4]],
myPlots[[5]], myPlots[[6]], myPlots[[7]], myPlots[[8]], nrow = 2)

这显然有效,但需要我枚举所有对象,我不知道会有多少对象。我试图通过写作来简化这一点
ggarrange(myPlots, nrow = 2)

但收到警告信息:
Warning message:
In as_grob.default(plot) : Cannot convert object of class list into a grob.

我做错了什么,我该如何解决?理想情况下,一行简单的代码将打印存储在 myPlots 中的所有图分两行。

提前致谢

托马斯·菲利普斯

最佳答案

ggpubr::ggarrange只是 cowplot::plot_grid() 的包装器.

但如果你想留下ggpubr ,那么您可以继续使用 ggarrange .并且您需要将所有绘图保存在一个列表中,并使用 plotlist争论。

library(ggpubr)
library(ggplot2)
library(purrr)

myplot <- function(color){
ggplot(iris,aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(color = color)
}
plot_list <- map(c("red","green","blue","black","orange"),myplot)


ggarrange(plotlist = plot_list,nrow = 2,ncol = ceiling(length(plot_list)/2))


enter image description here

关于r - 在页面上布置多个 ggplot 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58124284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com