[R] problems with saving plots from loop

Sarah Goslee sarah.goslee at gmail.com
Tue May 21 20:13:07 CEST 2013

I don't think the problem is with the plotting, I think the problem is
with your loop. R isn't selecting random numbers, it's doing exactly
what you told, but what you told it doesn't make sense. Hint: use the
same loop but simply print out i at each iteration.

Here's a better way to do it, assuming you really want to have a loop:

 for (i in 2:201){                #first column is concentration
   pdf(paste("plot_",i,".pdf",sep = ""))
   plot(mydata[,i] ~concentration,log='x')

On Tue, May 21, 2013 at 11:57 AM, SonaJ <184544 at mail.muni.cz> wrote:
> Greetings,
> I cannot find solution for this problem (I was searching on web, but without
> success):
> I want to plot dose-response models for one concentration and many responses
> (lets say 200) and I don´t want to do it manually.
> So I use loop for this:
> for (i in mydata[,2:201]){                #first column is concentration
> pdf(paste("plot_",i,".pdf",sep = ""))
>     plot(i~concentration,log='x')
>     ht <- seq(0,5000,1)
>     lines(ht, predict(selectedmodel, data.frame(concentration = ht)))
>                                                            #I previously
> selected appropriate model for each column within loop
>                                                            #(I don´t want to
> write here the whole loop with model selection)
>     dev.off()
> }
> This seems, that it works (I obtain many pdf files with plots), but there is
> problem with names of these files. R uses random numbers as names , but
> after some files (more than 49) one of the selected names is '0' . And after
> few other files, there is the same name ('0') again. So the previous file is
> overwritten and it is lost. And this is repeated many times (so many plots
> are lost). And there is also name '1' doing the same thing...
> I really do not know how to solve it...
> I also tried
> pdf(paste("plot_",names[i],".pdf",sep = ""))
> But it does not help (still random number as names are selected) ...
> I would be really happy if someone could help me with this...
> Thank you very much,
> Sona

Sarah Goslee

More information about the R-help mailing list