我在循环中生成一组ggplot对象,如下所示:
根据数据集的不同,myPlots中可能有4到10个图表。现在我想把它们打印在一页上,分为两行,并尝试了各种方法,但成功程度不同。最有希望的方法是:
我做错了什么,我该如何修复这个问题?理想情况下,一行简单的代码将会打印出存储在myPlots中的所有图表,并分为两行显示。
谢谢提前帮助。
Thomas Philips
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中的所有图表,并分为两行显示。
谢谢提前帮助。
Thomas Philips
ggarrange(plotlist = myPlots, nrow = 2, ncol = ceiling(length(myPlots)/2))
时,我得到了我想要的结果。非常感谢您的快速回复!!! - Thomas Philipsggarrange(plotlist = myPlots, nrow = 2, ncol = ceiling(length(myPlots)/2))
时,我得到了我期望的8个图形。然而,如果我源代码整个脚本,就不会出现任何图像。更令人困惑的是,如果我把光标放在该行上并单击运行,我就能得到这些图形。是否需要执行其他命令才能在源代码时显示它? - Thomas Philipsdev.off()
开始运行脚本rm(list = ls(all = TRUE)) #清除工作区中的所有对象 if(!is.null(dev.list()["RStudioGD"])) dev.off() #清除所有图形
但它无法显示garrange
的结果。当我单独运行这一行时,第一次会收到警告:Warning message: In doTryCatch(return(expr), name, parentenv, handler) : display list redraw incomplete
这个警告让你想起了什么吗? - Thomas Philips