I am trying to save multiple ggplot objects (choropleth maps) into the Global Environment for subsequent use. My idea is to get the individual ggplots named as "Plot1", "Plot2", .... I am using the following code (simplified, as the choroplets have a complicated syntax):
for( i in 1:length(VARIABLES)) {
plotname <- paste("Plot",i, sep = "")
#
plotdata <- melt(data = map.df, id.vars = c(....), measure.vars = VARIABLES[i])
#
eval(plotname) <- ggplot( .... multi-line command.... )
}
The problem is with the "eval(plotname)" part: If I delete it, the loop runs OK, but only the last plot is returned.
I have tried several do.call(), eval() and parse() combinations but I was not able to do what I wanted: That is, to use the Plot1, Plot2, etc. as names of individual ggplot objects to be saved.
Usually I get errors such as
Error in do.call("eval", plotname) <- ggplot(map.df.l, aes(long, lat, :
target of assignment expands to non-language object
I have searched stackoverflow, found Multiple ggplots in for loop but was not able to amend the code - I need to save the plots, not to print them.
Probably I am doing some trivial mistake (I'm not that much of a programmer)... Any comments and suggestions would be highly apreciated. Thank you.