Save ggplot within a function - r

I'm trying to save a ggplot within a function using graphics devices. But I found the code produces empty graphs. Below is a very very simple example.
library(ggplot2)
ff <- function(){
jpeg("a.jpg")
qplot(1:20, 1:20)
dev.off()
}
ff()
If I only run the content of the function, everything is fine. I know that using ggsave() will do the thing that I want, but I am just wondering why jpeg() plus dev.off() doesn't work. I tried this with different versions of R, and the problem persists.

You should use ggsave instead of the jpeg(); print(p); dev.off() sequence. ggsave is a wrapper that does exactly what you intend to do with your function, except that it offers more options and versatility. You can specify the type of output explicitly, e.g. jpg or pdf, or it will guess from your filename extension.
So your code might become something like:
p <- qplot(1:20, 1:20)
ggsave(filename="a.jpg", plot=p)
See ?ggsave for more details
The reason why the original behaviour in your code doesn't worked is indeed a frequently asked question (on stackoverlflow as well as the R FAQs on CRAN). You need to insert a print statement to print the plot. In the interactive console, the print is silently execututed in the background.

These plots have to be printed:
ff <- function(){
jpeg("a.jpg")
p <- qplot(1:20, 1:20)
print(p)
dev.off()
}
ff()
This is a very common mistake.

Related

Automatically calling dev.off after plot in R

I'm looking for a way to automatically call dev.off() to flush a plot to disk when someone calls plot() or ggplot().
Is this possible? RStudio looks to automatically load the plot, how does that work?
Edit: Add possible approach for ggplot.
Note that the reason that plot, in particular, doesn't automatically call dev.off() is that it's very common -- after calling plot -- to add additional material (annotations, supplementary plotting features, titles, legends, etc.) with additional calls before calling dev.off() to finalize the output.
But, if you really want plot to finalize the plot and prevent any further content from being added, you can do it by redefining plot:
plot <- function(...) {
graphics::plot(...)
dev.off()
}
Note that for window-based graphics devices (e.g., x11), the plot will briefly flash on the screen before disappearing, since dev.off() closes the window, but it should work fine for files:
> png("plot.png")
> plot(1:10,runif(10))
null device <-- proof that dev.off() was called
1
>
For ggplot2, I guess your best bet is to override the print method (which is how a plot normally gets displayed on the screen). So, if you define:
print.ggplot <- function(...) {
ggplot2:::print.ggplot(...)
dev.off()
}
then:
> png("plot.png")
> ggplot(mapping=aes(x=1:10,y=1:10))+geom_line()
[[ print method is implicitly called here ]]
> dev.off() # to prove that dev.off() was already called
Error in dev.off() : cannel shut down device 1 (the null device)
>
appears to work the way you want.
I don't use RStudio, so I'm not sure what it's doing differently, but I'm going to guess that it intercepts the graphics commands in such a way that it simultaneously shows the "plot in progress" before the file is finalized, rather than actually displaying the file (which, for bitmapped graphics, can't be written out at all until the plot is completely finished and dev.off() is called).

Why am I unable to see multiple plots appear with a for loop?

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()
}

R: Bitmap output in PDF

A lot of the time, I find it very useful to output graphics with pdf() as it allows me to scroll through pages and observe subtle differences (e.g. the page numbers may correspond to a particular parameter in a simulation).
Sometimes if the plot is quite packed with information, the fact that the PDF is a vector graphic means that it takes a long time to load in a PDF reader and is useless for scrolling through pages. I could plot with png(), but this would result in many image files.
My ideal solution would be to have a device that will plot a bitmap graphic (e.g. PNG) to a PDF.
I have read that cairo_pdf() outputs to a bitmap sometimes? Or I could write something that outputs to PNG, then combines these all together into a PDF?
Any other thoughts? Or does anyone have a solution for this already?
UPDATE: have now added method based on readPNG() as suggested in comments above. It's a bit slower (3s vs 9s) and seems to result in slightly larger file sizes than ImageMagick. rasterImage() interpolation makes no difference to filesize or timing, but alters the appearance slightly. If it's FALSE, then it looks the same as ImageMagick
I have just come up with the following solution using ImageMagick. It's not perfect, but it seems to work well so far.
png2pdf <- function(name=NULL,removepngs=TRUE,method="imagemagick",pnginterpolate=FALSE){
# Run the png() function with a filename of the form name%03d.png
# Then the actual plotting functions, e.g. plot(), lines() etc.
# Then dev.off()
# Then run png2pdf() and specify the name= argument if other pngs exist in the directory
# Need to incorporate a way of dealing with non-square plots
if(is.null(name)){
names <- list.files(pattern="[.]png")
name <- unique(sub("[0-9][0-9][0-9][.]png","",names))
if(length(name)!=1) stop("png2pdf() error: Check filenames")
}else{
names <- list.files(pattern=paste0(name,"[0-9][0-9][0-9][.]png"))
}
# Can change this to "convert" if it is correctly in the system path
if(method=="imagemagick"){
cmd <- c('C:\\Program Files\\ImageMagick-6.9.0-Q16\\convert.exe',names,paste0(name,".pdf"))
system2(cmd[1],cmd[-1])
}else if(method=="readPNG"){
library(png)
pdf(paste0(name,".pdf"))
par(mar=rep(0,4))
for(i in 1:length(names)){
plot(c(0,1),c(0,1),type="n")
rasterImage(readPNG(names[i]),0,0,1,1,interpolate=pnginterpolate)
}
dev.off()
}
if(removepngs) file.remove(names)
}

Create pdf and png simultaneously

Is there any way in R to create a png and a pdf simultaneously without having to run the same code twice? I'd like both a raster and vector version of some figures to be produced without having to do an external conversion and without rerunning the code if possible. dev.copy2pdf sounds like what I need, but it seems that it only works from interactive devices.
Here's an example of what I'm doing:
pdf("temp.pdf")
plot(1:10)
#more drawing here
dev.off()
png("temp.png")
plot(1:10)
#more drawing here
dev.off()
Which I'd like to shorten to:
start()
plot(1:10)
#more drawing here
saveToPDF()
saveToPNG()
There is an existing function dev.copy2pdf(), which does just what it says it will. There is also a function dev.print() which looks like it would make copying to a "*.png" file easy, but is in my experience pretty hinky.
The following two functions work well and have the added advantages of being uniformly named, and of taking all of their arguments in the same order as the familiar and related pdf() and png() functions:
saveToPDF <- function(...) {
d = dev.copy(pdf,...)
dev.off(d)
}
saveToPNG <- function(...) {
d = dev.copy(png,...)
dev.off(d)
}
## Try them out
plot(rnorm(99), col="red")
saveToPDF("my.pdf", height=4,width=7)
saveToPNG("my.png", height=600, width=400)

Saving plot as pdf and simultaneously display it in the window (x11)

I have written a function that creates a barplot. I would like to save this plot as a pdf as well as display it on my screen (x11) when applying this function. The code looks like this.
create.barplots <- function(vec)
{
x11() # opens the window
### Here is a code that creates a barplot and works perfectly
### but irrelevant for my question
dev.copy(pdf("barplots.table.2.pdf")) # is supposed to copy the plot in pdf
# under the name "barplots.table.2.pdf"
dev.off() # is supposed to close the pdf device
}
This creates the following error: 'device' should be a function
When I modify the code to:
create.barplots <- function(vec)
{
x11()
### Here is a code that creates a barplot and works perfectly
### but irrelevant for my question
dev.copy(pdf) # This is the only difference to the code above
dev.off()
}
R displays the plot and creates a file called Rplots.pdf. This is a problem because of several reasons.
I also tried to open the devices the other way around. First open the pdf device, than copy the content of the pdf device into the x11 device, than set the pdf device as active and than close the pdf device. The code here looks like this:
create.barplots <- function(vec)
{
pdf("barplots.table.2.pdf") # open the pdf device
### Here is a code that creates a barplot and works perfectly
### but irrelevant for my question
dev.copy(x11) # copy the content of the pdf device into the x11 device
dev.set(which = 2) # set the pdf device as actice
dev.off() # close the pdf device
}
The problem here is that the wondow that is supposed to display the plot is empty!
To sum up, I have two questions:
1) How to save a plot as pdf and display it in x11 simultaneously? And
2) How to save the plot not in the working directory somewhere else?
EDIT
The solutions above work great. But I still do not understand why
pdf("barplots.table.2")
barplot(something)
dev.copy(x11)
displays an empty grey window instead of copying the content of the pdf device in the window device! I also tried
pdf("barplots.table.2")
barplot(something)
dev.copy(window)
In which I failed as well...
How about:
create.barplots <- function(...) {
x11()
plot.barplots(...) # create the barplot
dev.copy2pdf(file = "path/to/barplots.table.2.pdf")
}
You can easily add arguments for pdf in the dev.copy call, like this:
create.barplots <- function(vec,dir,file)
{
windows()
plot(vec)
dev.copy(pdf,file=paste(dir,file,sep="/")
dev.off()
}
dev.copy() has a ... argument to pass arguments to the pdf function, see also ?dev.copy. Alternatively you can use dev.copy2pdf , as Max told you. I'd also advise you to use windows() instead of x11(), otherwise you might have trouble with the font families. The defaults for x11 and pdf don't always match.
To save a file in another directory, just add the full directory (eg with paste, like in the function above)
As I mentioned in a previous post, you may consider my knitr package; if you use it in an interactive R session, you will be able to see the plots in a window and save them to pdf without any hacks (it is the default behavior). I still need a lot of efforts on the documentation and demos, but it should be able to work with an Rnw document. The main reason that you can both see the plots and save them in knitr is, knitr is very different with Sweave in design -- the graphical device is opened after the code is evaluated, so your plots will not be hidden in an off-screen device. Again, I need to warn you that it is highly experimental at the moment.
Following works nicely for me when called from inside functions. Call it after the plot code:
pdf2 <- function (file = "plot.pdf", w = 10, h = 7.07, openPDF = FALSE)
{
dev.copy2pdf(file = file, width = w, height = h, out.type = "pdf")
if(openPDF) browseURL(file)
}
NB. openPDF may only work in Windows with full (not relative) file path.
Based on the answer by Max Gasner, I wrote this helper function which allows to quickly switch from displaying and not. The argument x is a plot object or the function that does the drawing.
savepdf<-function(x, file, display=TRUE) {
if (display){
x;
dev.copy2pdf(file=file)
}
else {
pdf(file=file)
x;
dev.off()
}
}
Example:
savepdf(plot(c(1,2,3)), file="123.pdf", display=F)

Resources