This is very elementary for those who use R... (But I do stats with Stata and Mplus.)
I develop many plots (638 in total) and want to save all in separate files. It worked well first, not now
for(i in 001:638){
## command for plot comes here, including mentioning of i ##
dev.copy(png,'plot-%d.png')
dev.off()
}
I want one file for each plot, but end up with a single plot file (plot_1.png), with only the last plot.
Christopher
png function will do this by default. For example, this will create 10 plots in your working directory.
png("plot-%d.png")
for(i in 1:10) plot(1:i)
dev.off()
You'll want to use one of the paste() functions to create your string.
Since you didn't provide a reproducible example I can only guess, but I think that something like this would probably work.
paste("plot",i,".png", sep = "")
in place of your current use of c style % replacement. So this
for(i in 001:638){
#command for plot comes here, including mentioning of i ##
dev.copy(png,paste("plot",i,".png", sep = ""))
dev.off()
}
Related
In the following example, I want to write the residuals plot of each model in a file. I do not need to see them in my display.
for (i in 1:500){
temp.model<-lme(as.formula(paste("Var",i) ~ X1*X2, sep=""), data = example, random=~1| Exp/Person)
jpeg(paste("C:/Myfolder", i, ".jpg", sep = ""), quality=50, bg="white")
plot(temp.model)
dev.off ()
graphics.off()
}
When I run this code without loop, I obtain what I want. However, it creates blank files within the loop.
Any ideas?
Thank you.
The answer is in the FAQ, FAQ 7.22 in fact. However this is not obvious until you realize that the plot.lme function from the nlme package uses lattice/trellis graphics to do the actual plotting (there are references on the help page for plot.lme, but not obvious).
The short form of the solution (but I still recommend reading the FAQ and the other documentation to fully understand the issue) is to wrap the plot in a print command.
This is my code which is part of a larger script.
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
png(paste(survPath,"/surv_",d1,".png",sep=""))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
}
I don't have a good idea of what this code does yet, so I'm trying to look at each individual plot to see what it is. The problem is, whenever I run this in the R command line in the terminal in linux, nothing appears. I have to use dev.off() multiple times and then rerun this code:
plot(survfit1)
for something to appear. How can I see all the plots?
Sounds like this is really what you want:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
x11() ## open up new graphical window for each plot (to avoid overwriting)
plot(survfit1,xlab="Years",ylab="Survival probability",
xmax=xmax1, main = d1) ## use different titles to distinguish those plots
}
This will produce plots on normal graphical windows.
If you want to use the original code, you'd better do this way:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
png(paste(survPath,"/surv_",d1,".png",sep=""))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
dev.off()
}
Then, have a look at the directory given by getwd(). All the plots are saved in png files.
Calling Sys.sleep(.1) might help during the for loop. Maybe try:
for(d1 in names(survD)){
survfit1 <- survfit(Surv(time=survD[[d1]][,"time"],
event=survD[[d1]][,"death"],type='right')~1)
Sys.sleep(.1)
png(paste(survPath,"/surv_",d1,".png",sep="", collapse="))
plot(survfit1,xlab="Years",ylab="Survival probability",xmax=xmax1)
dev.off()
}
In the following example, I want to write the residuals plot of each model in a file. I do not need to see them in my display.
for (i in 1:500){
temp.model<-lme(as.formula(paste("Var",i) ~ X1*X2, sep=""), data = example, random=~1| Exp/Person)
jpeg(paste("C:/Myfolder", i, ".jpg", sep = ""), quality=50, bg="white")
plot(temp.model)
dev.off ()
graphics.off()
}
When I run this code without loop, I obtain what I want. However, it creates blank files within the loop.
Any ideas?
Thank you.
The answer is in the FAQ, FAQ 7.22 in fact. However this is not obvious until you realize that the plot.lme function from the nlme package uses lattice/trellis graphics to do the actual plotting (there are references on the help page for plot.lme, but not obvious).
The short form of the solution (but I still recommend reading the FAQ and the other documentation to fully understand the issue) is to wrap the plot in a print command.
I am still new to R and I have searched around for a solution to my simple question, but I haven't found an answer that I've been able to get to work. I am looking to use a previously identified variable per data set, here variable=SNPname to include in script for automated generation of graph output in png format.
I am using this to generate a kmeans plot and have:
(cl <- kmeans(FilteredData[,6:7], 5, nstart=25))
png("C:/temp/$SNPnamegraph1.png") #SNPname to include in filename
plot(FilteredData[,6:7], col=cl$cluster)
points(cl$centers, col=1:5, pch=8)
dev.off()
where I want to include that variable in line 2 at the beginning of the file name. Is there a simple way to do this that I am just missing?
Close, you're just missing the use of paste() and setwd()
setwd("C:/temp/") # use this to set where you want things saved
...
c1 <- kmeans...
png(paste(SNPname, " graph1.png", sep=""))
...
If it's in a loop of some kind, you might need to use SNPname[loop_var]
Is there a way to have an R Device (postscript would be great) write the output into a variable instead of a file?
For example I know this:
postscript(file="|cat")
plot(1:10)
dev.off()
Will send the postscript text to STDOUT. How can I get that text into a variable within R?
I've had success in getting the Binary of a plot into an R variable as a string. Its got some read/write overhead. In the snippet below, R saves the plot as a temp file and reads it back in.
## create a plot
x <- rnorm(100,0,1)
hist(x, col="light blue")
## save plot as temp file
png(filename="temp.png", width=500, height=500)
print(p)
dev.off()
## read temp file as a binary string
plot_binary <- paste(readBin("temp.png", what="raw", n=1e6), collapse="")
Maybe this is helpful to you.
postscript takes a command argument, hence postscript(file="",command="|cat")
Why on earth would you want to do that? R is not a very good system for manipulating Postscript files. If nothing else, you can use tempfile() to write the image to a file, which you can then read in using standard file functions. If you wanted to be fancy, you could perhaps use fifo() pipes, but I doubt it'll be much faster. But I suspect you'd be better off with a different approach.
You should be able to use a textConnection as follows.
tc <- textConnection("string", "w")
postscript(tc)
plot(1:10)
dev.off()
But string remains blank - maybe a bug?