13

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)
4
  • does it help if you restart R? how about changing the filename to "/tmp/a.png"?
    – Alex Brown
    Nov 24, 2010 at 18:46
  • No, running gc(), restarting, or saving under a different name doesn't work.
    – James
    Nov 24, 2010 at 18:51
  • 2
    Can't reproduce on Windows Vista and R 2.11.1 with the examples from the help files. Seems a problem related to your computer or the graph. Is it possible to get a reproducible example?
    – Joris Meys
    Nov 24, 2010 at 22:24
  • I can reproduce with some adjustments to your code, and suspect this is a bug. See the second edit to my answer.
    – Joris Meys
    Nov 28, 2010 at 23:43

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

16

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.

5
  • Thanks Joris, I've included an example now. I managed to get it to work for 7.5x10 and this allowed enough space to get labels printed properly. I suppose it probably is a graphics card issue, but its still a little small for that.
    – James
    Nov 28, 2010 at 21:34
  • I still get the problem using R 2.15.2 32-bit. I can "solve" the issue by restarting my computer and only loading R up. I suspect that this is therefore a logical memory address space issue - ie the OS cannot give R enough contiguous memory to create the image.
    – James
    Oct 24, 2013 at 10:20
  • I tried it again on my system using R 3.0.2, but I fail to reproduce. I reran my code and I can let i reach 38, at which the dimensions are just too big (50 inches is too much for the device). If I rerun my example code, it does exactly the same again: run until i reaches 38, and stop with the same error. So I guess the problem is not R (I'm running 32bit as well).
    – Joris Meys
    Oct 25, 2013 at 10:00
  • Is your OS 32-bit? I think 32-bit R in a 64-bit OS will still get some benefits of the larger address space available to the OS.
    – James
    Oct 25, 2013 at 10:02
  • My OS is 64bit indeed. The original problem was due to some memory leak in R, which has been solved. I do get the error "cannot allocate bitmap" if I set the dpi to far higher, or if I lower the memory devoted to R. Seems like an issue that's unrelated to R, but to your computer. You have to remember that in order to make a .png file, R first creates a bitmap which needs quite some memory...
    – Joris Meys
    Oct 25, 2013 at 12:02
0

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.

0

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")
2
  • Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Aug 30, 2021 at 11:28
  • Thanks for your comment, I added two examples about using ggsave from ggplot2 package Aug 31, 2021 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.