I have several variables that I have to visualize, each in a different plot. I have more than 20 variables, and I want to have each plot stored as an element, so I can create the final figures only with the ones that are useful for me. That's why I thought creating plots in a loop would be the best option.
I need several kinds of plots, but for this example, I will use boxplots. I want to stick to ggplot because it will allow me to tweak my graphs easily later.
Let's say this is my data:
a<-(rnorm(100,35,1))
b<-(rnorm(100,5,1))
c<-(rnorm(100,40,1))
mydata<-data.frame(a,b,c)
I would like to have histograms for each variable named Figure 1, Figure 2, and Figure 3.
I'm trying this, but with little experience with loops, I don't know where I'm wrong.
variable_to_be_plotted<-c("a","b","c")
for (i in 1:3) {
paste("Figure",i)<-print(ggplot(data = mydata, aes( variable_to_be_plotted[i] )) +
geom_boxplot())
}