I want to create a series of plots, and then I plan to use ggarrange to put them in a single picture.
I have a code which works but assigning plots to a vector corrupts the plots except the last one.
What should be changed for this to work?
In the example below, print(pp) prints correctly all plots, however Plots contains corrupted plots (last plot is somehow added to previous ones)
library(tidyverse)
mtcars$names <- rownames(mtcars)
Plots <- vector(length=32,"list")
for (i in 1:32) {
x <- mtcars[[i,12]]
y <- mtcars[[i,3]]
pp <- ggplot() +
aes(x=x , y=y)+
geom_point()+
labs(title="",x="",y="")+
geom_text(aes(label=round(y,2)),hjust=-0.2, vjust=-0.2, color="black")+
theme_bw()+
geom_hline(yintercept=0, linetype="dashed", color = "red", size= 0.5)+
annotate("text",x=min(x),y= 0, hjust = 2, vjust = -0.2, label = "zero line", color = "red")
print(pp) #works fine, but I need to collect all plots to use them further with ggarrange()
Plots[[i]] <- pp
}
Plots #has corrupted plots