0

I would like to create graphs for multiple variables and save the graphs without have to type the same code over and over.

Variables: Var1, Var2, Var3

vars <- c(   "Var1", "Var2", "Var3"   )
for (v in vars) {
Graph_v <-  D %>%                               
  ggplot(mapping = aes(x= X, y= v)) +   geom_point(aes(x= X, y= v)) +
  stat_summary(
    fun.data = mean_se,
    geom = "line", size = 1)
}

My main problem is to safe the single graphs under different names.

Thanks for your help. Hanna

R will only safe the latest graph created because the name of the graph does not change.

Hanna
  • 1
  • 1

1 Answers1

0

Are you just trying to save them in a list?

library(tidyverse)

vars <- c("cyl", "hp")

plot_list <- list()
for(i in seq_along(vars)){
  plot_list[[i]] <- ggplot(mtcars, aes(!!sym(vars[[i]]), mpg))+
    geom_point()
}

plot_list
#> [[1]]

#> 
#> [[2]]

AndS.
  • 7,748
  • 2
  • 12
  • 17