Skip to content

Instantly share code, notes, and snippets.

@edavidaja
Created February 28, 2020 17:01
Show Gist options
  • Save edavidaja/254bf56c7776931490b0a78d252d436a to your computer and use it in GitHub Desktop.
Save edavidaja/254bf56c7776931490b0a78d252d436a to your computer and use it in GitHub Desktop.
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment