[R] problems with saving plots from loop

arun smartpink111 at yahoo.com
Tue May 21 20:36:50 CEST 2013


dat1<- as.data.frame(matrix(c(rep(c(5,10,15,20,25),each=3),sample(1:20,15,replace=TRUE),sample(15:35,15,replace=TRUE),sample(20:40,15,replace=TRUE)),ncol=4))
names(dat1)[1]<- "concentration"
lapply(seq_len(ncol(dat1[,-1]))+1,function(i) {x1<- cbind(dat1[,1],dat1[,i]);colnames(x1)<-c(colnames(dat1)[1],colnames(dat1)[i]);pdf(paste("plot_",colnames(x1)[2],".pdf",sep=""));plot(x1[,1],x1[,2],xlab=colnames(x1)[1],ylab=colnames(x1)[2]);dev.off() })

----- Original Message -----
From: SonaJ <184544 at mail.muni.cz>
To: r-help at r-project.org
Sent: Tuesday, May 21, 2013 11:57 AM
Subject: [R] problems with saving plots from loop


I cannot find solution for this problem (I was searching on web, but without

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 = ""))    
    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)

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,

View this message in context: http://r.789695.n4.nabble.com/problems-with-saving-plots-from-loop-tp4667616.html
Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_V2.pdf
Type: application/pdf
Size: 4555 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130521/40c28302/attachment.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plot_V3.pdf
Type: application/pdf
Size: 4570 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130521/40c28302/attachment-0001.pdf>

More information about the R-help mailing list