Maximilian Lklweryc
2012-Nov-21 18:07 UTC
[R] plotting 1000 simulations, error message: plot.new has not been called yet
Hi, I know this is not a mailing list for r, but I posted my question on several help pages and did not get any help. I really don't know how to solve my problem, maybe you could help me? want to simulate stock paths. I have simulated 1000 paths with 22 trading days (1 starting value). Now I want to include it into my presentation, but animated, so I need the png files. I want to create 1000 png files, starting with the first stock path, then the second and so on. 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. How can I change that the error does not appear anymore? Thanks!
Michael Weylandt
2012-Nov-21 23:52 UTC
[R] plotting 1000 simulations, error message: plot.new has not been called yet
On Nov 21, 2012, at 6:07 PM, Maximilian Lklweryc <maxlklweryc at gmail.com> wrote:> Hi, > I know this is not a mailing list for r,It's not? I'm confused then!> but I posted my question on > several help pages and did not get any help. I really don't know how > to solve my problem, maybe you could help me? > > want to simulate stock paths. I have simulated 1000 paths with 22 > trading days (1 starting value). Now I want to include it into my > presentation, but animated, so I need the png files. > > I want to create 1000 png files, starting with the first stock path, > then the second and so on. > > 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() > }Move plot inside the loop just after jpeg() MW> > (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. > > How can I change that the error does not appear anymore? Thanks! > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Greg Snow
2012-Nov-23 00:49 UTC
[R] plotting 1000 simulations, error message: plot.new has not been called yet
Why are you using jpeg to create files with a .png extension? wouldn't it make more sense to use the png function? There are a couple of options, you could plot using the matplot function (with different subsets in the loop). Or you can start an initial empty plot and use lines to add to it, then use dev.copy to copy the current version to a file. Or you can look at the animation package. Or you could do nested loops, but hopefully one of the above is better than that approach. On Wed, Nov 21, 2012 at 11:07 AM, Maximilian Lklweryc <maxlklweryc@gmail.com> wrote:> Hi, > I know this is not a mailing list for r, but I posted my question on > several help pages and did not get any help. I really don't know how > to solve my problem, maybe you could help me? > > want to simulate stock paths. I have simulated 1000 paths with 22 > trading days (1 starting value). Now I want to include it into my > presentation, but animated, so I need the png files. > > I want to create 1000 png files, starting with the first stock path, > then the second and so on. > > 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. > > How can I change that the error does not appear anymore? Thanks! > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]