9
quick.hist<-ggplot(ufo.us, aes(X=DateOccured))+geom_histogram()+scale_x_date()
ggsave(plot=quick.hist,filename= "../images/hist.png",height=6, width=8)

file format is simple:

it contains 2 dates,city,state,duration

I am getting error during plot:that is during ggsave()

Error in grDevices::png(..., width = width, height = height, res = dpi,  
  unable to start png() device

In addition: Warning messages:

 In grDevices::png(..., width = width, height = height, res = dpi,  
  unable to open file '../images/hist.png' for writing

 In grDevices::png(..., width = width, height = height, res = dpi,  
  opening device failed

I am very new to R and getting error. I have just started copying the book code for histogram chart, first chapter from book machine learning hacker perspective.

Rimian
  • 36,864
  • 16
  • 117
  • 117
user3589052
  • 91
  • 1
  • 2
  • try another file format, e.g. pdf, and a simpler file path e.g. "hist.pdf" – baptiste Apr 30 '14 at 12:27
  • Have you got the file open in another program? – James Apr 30 '14 at 12:39
  • 8
    @baptiste - this is almost certainly a directory permissions (or nonexistent directory) problem, not png vs pdf. – Carl Witthoft Apr 30 '14 at 13:07
  • @Carl Witthoft I'm having the same issue. And it seems to be **png vs pdf**. Pdf save works ok – ikashnitsky Feb 26 '16 at 15:10
  • @Ilya if you can repeat the issue, it's probably worth sending your example, along with your operating system, R-version, etc. to the maintainer of `grDevices` – Carl Witthoft Feb 26 '16 at 17:07
  • 1
    @Carl Witthoft I will. In a [related post](http://stackoverflow.com/questions/4270349/ggsave-png-error-with-larger-size) I found how to get around the problem. The devil is in `dpi` parameter of `ggsave`. The default is 300. Empirically I chose 192, as 256 produced the similar error. – ikashnitsky Feb 26 '16 at 19:36
  • check if 'images' folder exists – volody Sep 24 '16 at 14:26

3 Answers3

3

I had the same problem recently. It occurs when compiling R from source without having the appropriate libraries installed. So R does not know how to talk to the png device at all.

In order to fix this problem just install the following packages (e.g. using Ubuntu16.04)

sudo apt install libcairo2-dev libjpeg9-dev

and run the ./configure again.

If those libraries were sufficient both PNG and cairo should be listed as Additional capabilities at the end of the configuration. So if none of them shows up using

cat config.log | grep skipped

you are good to go.

2

I had the same issue. In my case, my working directory (check getwd) was not the right one so the filepath didn't make sense.

user3507584
  • 3,246
  • 5
  • 42
  • 66
0

I had this same problem as the OP. It appeared to be an error from ggsave() but I traced it back to a ggarrange() call. I was hitting my limit of open devices (~64) and needed to call dev.off() following each ggarrange() call. This post was helpful: Too many open devices r

DirtStats
  • 559
  • 9
  • 29