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)
It has happened to me with png, jpeg and pdf extensions in Windows (32 bits). After a bit of research, I discovered that the cause was that I was trying to save them in the hard disk directly:
It seems that RStudio has not administrator permissions to write directly into C:/. I have changed the folder to Desktop and now everything is working fine.
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:
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 withpng()
anddev.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 megc()
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.