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 -- 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.
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 http://www.functionaldiversity.org
Hi, Try: set.seed(28) 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() }) A.K. ----- Original Message ----- From: SonaJ <184544 at mail.muni.cz> To: r-help at r-project.org Cc: Sent: Tuesday, May 21, 2013 11:57 AM Subject: [R] problems with saving plots from loop 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 -- 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 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. -------------- 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>