'R for-loop with assign & paste0 [duplicate]

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source