library(ggplot2)
library(purrr)
split_iris <- split(iris, 1:3)
map2(
split_iris, names(split_iris), ~ {
lm(Sepal.Length ~ Sepal.Width, data = .x)
p <- ggplot(.x, aes(Sepal.Width, Sepal.Length, color = Species)) + geom_point()
print(p)
ggsave(plot = p, filename = paste0("plot_", .y, ".png"))
})
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> $`1`
#> NULL
#>
#> $`2`
#> NULL
#>
#> $`3`
#> NULL
list.files()
#> [1] "plot_1.png" "plot_2.png" "plot_3.png"
#> [4] "reprex_reprex.R" "reprex_reprex.spin.R" "reprex_reprex.spin.Rmd"
Created on 2020-02-28 by the reprex package (v0.3.0)