7

I've updated my ggplot2 library and I am now having a problem where after calling ggsave, the graphics device no longer displays the plot. I'm unable to resolve the issue meaning I cannot export the plots as needed. Please help.

Searching the web has revealed some mention of problems with dpi mismatches, but setting or changing the dpi appears to have no effect. This problem appears to have begun after doing an update of the ggplot2 library.

The following code reproduces the problem:

library(ggplot2);
df <- read.table(textConnection('value,value1 
 1,1 
 2,2
 3,3 '),header=TRUE,sep=',');

ggplot(df,aes(x=value,y=value1)) +   geom_point();

setwd('c:/temp'); 
ggsave('test.png');

After running, the output is blank, and the graphics device no longer displays the plot. Any subsequent plots are not displayed on the graphics device.

After running dev.off(), new plots are displayed okay, until ggsave is called again.

sessionInfo() reports the versions of R and libraries as:

R version 3.0.0 (2013-04-03)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_New Zealand.1252  LC_CTYPE=English_New Zealand.1252   
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C                        
[5] LC_TIME=English_New Zealand.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plyr_1.8        ggplot2_0.9.3.1 RODBC_1.3-6    

loaded via a namespace (and not attached):
 [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       grid_3.0.0         gtable_0.1.2      
 [6] labeling_0.1       MASS_7.3-26        munsell_0.4        proto_0.3-10       RColorBrewer_1.0-5
[11] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_3.0.0   

The problem occurs in 2.15.0 and 2.15.3 of R also.

EDIT: changed code so data would load using sep=',' rather than whitespace

ADDITION: further testing reveals that this may in fact be a bug with RStudio and how it is displaying the graphics device after ggsave, rather than R itself. Running the above script in RGui does not reproduce the problem, only in RStudio V0.97.336 + V0.97.449.

Jayden
  • 2,656
  • 2
  • 26
  • 31
  • why are semi-colons bad in R? – Jayden May 10 '13 at 00:57
  • They're completely unnecessary if you're putting commands on separate lines – Dason May 10 '13 at 01:25
  • 1
    Running this does generate an empty graphics device for me, but I don't experience any of the other problems. Subsequent plots display on the newly opened graphics device just fine. (I'm on OS X.) – joran May 10 '13 at 01:48
  • 1
    I can't replicate on windows XP either. – mnel May 10 '13 at 02:57
  • 1
    I couldn't replicate on Windows 7 – Dason May 10 '13 at 03:06
  • 1
    Thanks for posting this. I had the same problem (on mac) and thought it'd be a painful one to try and get people to understand what is going on. I find I have to use dev.off() every now and again to flush it out. But as suggested in the answer, executing the lines separately seems to work too. – nzcoops Oct 08 '13 at 07:30
  • 2
    6 years later, I came across the same error `Unknown graphics device` while using `ggsave`function. For me, I had the error just because I missed the image extension `.png` in the `filename` argument . Posting this in here, just incase someone finds it useful. – mukund Dec 19 '19 at 22:29

1 Answers1

5

Seems to be an issue with RStudio. The issue is described here and here.

The workaround is to install RStudio V0.97.311, or ensure that you execute the ggplot and ggsave lines separately (i.e. don't select and run together in a block with control+enter).

Jayden
  • 2,656
  • 2
  • 26
  • 31