I have to consecutively plot lots of variables against a fixed one using ggplot2 in R. For doing so, I wrote a for cycle generating the code lines like this:
l <- "ggplot(data = db)+geom_line(aes(x = asofdate, y = copper), colour = 'tomato')+geom_line(aes(x = asofdate, y = db[['"
x <- vector()
for (j in (3:ncol(db))){
n <- colnames(db[,j])
k <- paste0(n, "']]), colour = 'blue')+labs(title = 'copper & ", n,"'); ")
x <- paste0(x,l,k)
}
eval(parse(text=substr(x, 1, (nchar(x)-1))))
Where in the for cycle I conclude each graph generation with a semicolon, so that R recognizes groups of code to be evaluated separately. At this stage the code is stored into a string. Then I use the function parse
and eval
to evaluate the string and execute it as code.
However, after I run the for cycle, in x
I have that each graph code is separated by a semicolon ;
while after using the function parse the semicolons become commas ,
.
This makes my for cycle useless, since R does not recognize different groups of code.
Any idea why this happens and how can I keep the semicolon after parse
?