Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggsave png error with larger size

Tags:

r

ggplot2

I'm saving a faceted ggplot2 plot which works fine to save at a smaller size, but fails when I want to increase it.

> ggsave("tst.png",height=6.75,width=9)
# works fine

> ggsave("tst.png",height=9,width=12)
Error in grDevices::png(..., width = width, height = height, res = dpi,  : 
  unable to start device
In addition: Warning messages:
1: In grDevices::png(..., width = width, height = height, res = dpi,  :
  Unable to allocate bitmap
2: In grDevices::png(..., width = width, height = height, res = dpi,  :
  opening device failed

I've saved pngs of this size before with ggsave, any ideas why its not working?

Reproducible example:

library(car)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)
like image 567
James Avatar asked Nov 24 '10 18:11

James


People also ask

What is DPI in Ggsave?

To go from the dimension in inches to a number of dots, ggsave uses the number of dots per inches (dpi). So if we create a plot in ggplot and save it with a dimension of 12 x 10 with the default dpi of 300 for ggsave, the file will be a matrix of (12 * 300) x (10 * 300) = 3600 x 3000 dots.

How do I save a ggplot as PNG?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot.

What is Ggsave?

ggsave() is a convenient function for saving a plot. It defaults to saving the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.

Where do Ggsave files go?

ggsave. ggsave works by default on the most recent graph that you've plotted. The only arugment it needs is a file name to save it as. By default, it will save to the directory that you're working out of.


1 Answers

NOTE : Using R 2.12.1 on Windows 7 64bit, this problem has vanished. If you run into this problem, first try updating your R version.

After the problem came up again in another question, I reran my test code on my new system to see if the bug was gone, and it is.


EDIT: The trick why underlying code could work is the fact that it uses a resolution of only 72 dpi and not 300dpi as is the standard in ggsave() I believe.

so ggsave("tst.png",height=9,width=12,dpi=72) could do the trick.

But you really must have a crazy plot if it can't take it. As far as I can guess, the problem is related to the graphics card (as derived from this message from prof. Ripley ).

If resolution is a problem, you could better go to vectorized formats like eps or pdf.


EDIT 2 :

Apparently, there is a bug somewhere involving some kind of memory leak maybe? Give following code:

library(car)
library(ggplot2)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)
setwd("G:/Temp")
i<-1
while(1){
  tryCatch(ggsave("tst.png",height=9+i,width=12+i),error=function(e) {print(i);stop(e);})
  i <- i+1
}

This runs fine for me until i reaches about 9, then I get the error you get. Every next attempt at running the code, starting again with i=1, gives the same error. Trying with png() and dev.off() gives again the same error. Seems like there is some part of a memory filling up and not being emptied, effectively preventing to get another png file saved. also for me gc() didn't do a thing. Even closing R and reopening again didn't work.

It is "solved" using ggsave("tst.pdf"), but the bug remains. I'd report to the R team.

like image 62
Joris Meys Avatar answered Oct 17 '22 10:10

Joris Meys