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 grDe开发者_如何学编程vices::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)
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.
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:
ggsave(paste("C:/how",eval(parse(text = "i")),eval(parse(text = "j")),".pdf",sep="_"),height=6.75,width=9)
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.
I faced that problem and I just typed
ggsave(plot_name, filename = "output_directory/xxxx.png")
it saved the file in the required directory without mentioning the path or the device.
I am using windows 10.
update:
I tried two examples to save plot using ggsave, and it worked well by using my solution where I included the plot and file name directly. or where I added path, filename, device.
It looks the problem is related to the path of the project directory on Rstudio is long so ggsave throughs that error.
library(ggplot2)
plot <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) + geom_point()
ggsave(plot, path = "01_Output/long path saving file for ggsave example/", filename = "iris3.png", device = "png")
ggsave(plot, filename = "01_Output/long path saving file for ggsave example/iris2.png")
精彩评论