Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

r - ggsave png error with larger size

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...