0

I have multiple objects created as var_1, var_2, var_3, ect... I am wanting to create qq plots for all of the variables and save the plots as p_1, and ect..., but not continually copy and paste. So I created for-loop for iteration as given:

for (i in c(1:3)) {
  assign(paste0("p", "_", i),
         ggplot()+
           geom_qq_line(aes(sample = get(paste0("var", "_", i))))+
           geom_qq(aes(sample = get(paste0("var", "_", i))))
         )
}

But, all of the plots end up being the same as p_3. Is there something I am missing?

  • 1
    The main problem is that `aes()` uses lazy evaluation and it captures the symbol `i` rather than the value and `i` only has one value at the end of the loop. Also `assign()` is really a headache to work with, better to use a named list. For example `p – MrFlick Mar 07 '22 at 04:51

0 Answers0