0

I want to create 1000 png files, while each file should contain one simulated stock pathes but also additional the others.

So I start with the first path, add a second to the plot, add the third and so on, so at the end I have a plot with 1000 simulations, here is my code:

for(i in 1:1000){
#jpeg(paste("1000s",i,".png",sep=""))
plot(c(1:23),matrix[,1],type="l",ylim=c(17,24))
lines(c(1:23),matrix[,i],type="l",col=i)
#dev.off()
}

Here is the problem, that each additional part disappears when the loop gets to the next value, so I tried:

plot(0,0 , xlim=c(1,23),ylim=c(17,24),xlab="",ylab="")
for(i in 1:1000){
jpeg(paste("1000s",i,".png",sep=""))
lines(c(1:23),matrix[,i],type="l",col=i)
dev.off()
}

I know this is not a working example, but my problem is just a logical one with the loop, I get the following error message when I the last code: plot.new has not been called yet

The matrix has 1000 columns and 23 row entries, this should be 1000 simulations of stock pathes for 22 trading days.

What do I have to change? Thanks!

2
  • Your first snippet of code works for me to create 1000 plots, each with two lines on them (the first column in the data, compared to another column). Is this not what you want? It's not clear what you do want - do you want each image to be built up so the third image has three lines, the fourth one four lines, etc? If so you will need a second loop within the first. Nov 29, 2012 at 9:43
  • Also, if you want to create PNGs you should use png() not jpeg(); and it is not a good idea to use "matrix" as the name of an object as this will mask the matrix() function. Nov 29, 2012 at 9:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.