我想使用ggsave()
从我的R环境中保存所有的图形。 我如何在R中保存图形列表,然后将该列表用作ggsave()
的输入?
我使用cars
获取了一些图形,这里有示例:
PlotA <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3)
PlotB <- ggplot(mtcars, aes(x=hp, y=mpg, color=cyl, shape=cyl)) +
geom_point(size=3) +
geom_smooth(method="lm", aes(fill=cyl))
PlotC <- ggplot(mtcars, aes(x=hp, y=mpg)) +
geom_point(size=3, aes(color=cyl, shape=cyl)) +
geom_smooth(method="loess", color="black", se=FALSE) +
geom_smooth(method="lm", aes(color=cyl, fill=cyl))
- 我的尝试:
saveplots <- list()
saveplots <- ls(pattern = 'Plot')
### Save pngs ###
for(i in 1:length(saveplots)){
ggsave(saveplots[[i]],
file=paste0("Total", saveplots,".png"),
width = 22, height = 11.5, units = "cm",
path = "plots/")
}