目前我希望使用 ggplot2 在同一窗口中绘制多个图。我在网上搜索了一下,发现gridExtra::grid.arrange
函数将完成这项工作。
为了让它工作,我必须将参数中的每个图包含为 gridExtra::grid.arrange(plot1,plot2,plot3,...,plotN)
。但是,如果我想更通用地使用此函数并想输入绘图列表,该怎么办? myList <- list(plot1,plot2,...plotN)
输入绘图列表,以unlist(myList)
的形式输入在函数中给出了一个错误。那么,这个问题会得到解决吗?
最佳答案
要使 grid.arrange() 正常工作,您需要显式定义 grobs 参数。
library(ggplot2)
library(gridExtra)
df <- data.frame(x = 1:100,
y1 = runif(100),
y2 = runif(100)^2)
plot_list <- list(
plot1 = ggplot(df, aes(x, y1)) + geom_point(),
plot2 = ggplot(df, aes(x, y2)) + geom_point()
)
gridExtra::grid.arrange(grobs = plot_list)
关于r - 如何在网格中绘制绘图列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843966/