0

I want to plot many different lines in one plot. I wrote the following code, but it creates new plots as new lines are added. How could I get only one final plot?

plot(Rp_cum_optimal,type="l",lwd=5, ylim=c(-1,max4plot)) #optimum solution plot
lines(cumsum(asset_returns[,2]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,3]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,4]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,5]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,6]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,7]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,8]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,9]),col="red") #S&P500 plot
lines(cumsum(asset_returns[,10]),col="red") #S&P500 plot
r2evans
  • 141,215
  • 6
  • 77
  • 149
user3203275
  • 195
  • 2
  • 11
  • 1
    Just a quick comment: instead of defining lots of separate `lines`commands you could define all of them in a `for` loop, thus: `for(i in 1:ncol(asset_returns[,2:10])){ lines(asset_returns[,i], col = "red") }` – Chris Ruehlemann Feb 23 '20 at 17:41
  • what is Rp_cum_optimal ? can you do dput(head(Rp_cum_optimal,10)) and paste the output as part of your post so we know what kind of object is there? – StupidWolf Feb 23 '20 at 18:00
  • Also `asset_returns`. Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Feb 23 '20 at 18:54
  • thanks for your comments. Could please someone write a sample code of how to plot many lines in 1 single graph? – user3203275 Feb 23 '20 at 19:07

0 Answers0