[英]extract plots from list R
I have made a function:我做了一个function:
mtcars_plot_fun <- function(cyl_in, gear_in) {
car_data <- mtcars %>%
filter(cyl == cyl_in,
gear == gear_in)
p <- car_data %>% ggplot(aes(x = disp, y = qsec, fill = gear)) +
geom_point()
print(p)
}
Then a loop that makes plots and stores them in the list object gg_obj
然后是一个循环,绘制并将它们存储在列表 object gg_obj
cyls <- c(4, 6)
gears <- c(3, 4)
for (cyl in cyls) {
for (gear in gears) {
gg_obj <- mtcars_plot_fun(cyl_in = cyl, gear_in = gear)
}
}
Now I want to extract the plots I need from gg_obj
and use grid.arrange()
, gridExtra
or similar to plot them.现在我想从gg_obj
中提取我需要的图并使用grid.arrange()
、 gridExtra
或类似于 plot 它们。 How can I pull out the plots I want, and store them as unique objects to plot in a grid?如何拉出我想要的图,并将它们作为唯一对象存储到网格中的 plot 中?
You can use do.call
to use your list of plots as arguments for grid.arrange
.您可以使用do.call
将您的绘图列表用作 grid.arrange 的grid.arrange
。
For example:例如:
do.call(grid.arrange, list.of.plots, ncol=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.