2

im having issues with making high quality pictures in R. I'm using ggplot2 and it seems that the standard way to save does not do the anti-aliasing satisfactory. I then looked around and figured that Cairo could do what I wanted. It doesnt though, it tells me that polygon edges are not found, and I have no idea what that means. Using Cairo to plot standard plots works.

Here is an example:

library(ggplot2)
library(Cairo)

#making data of sorts
DF = data.frame("x" = rep(c(10,20),each=100), "y"= rnorm(200))
g = ggplot(DF, aes(x=x,y=y) ) +geom_point(size=3 , position = position_jitter(w = 0.1, h = 0) ) 

#standard plotting works fine
print(g)

#Cairo plotting does not
CairoPNG("overlay.png", width=4.5, height=4)
print(g)
dev.off()

###Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
###polygon edge not found

Im running windows 7, and i have tried updating all packages, reinstalling R and restarting.

MLS
  • 21
  • 1
  • 3
  • Yes i had already had a look at that, but i am not entirely sure about what Gtk+ does, or how it works with R. I have now installed it - could you possibly walk me through the steps? – MLS Dec 09 '13 at 17:39
  • Also, it appears that I can get cairo_pdf and cairo_ps to work (form the cairoDevice package), so i might just have to resort to that. Does not explain what the issue with Cairo is, though. – MLS Dec 09 '13 at 17:55
  • Found a work around; save the plot as a pdf using the export function in RStudio (using pdf(...) replaces all dots with q's in my plots? ), download Inkscape, use it to convert pdf to 'enhanced metafile' to insert into Word, and PNG for submissions. Not a solution, though. – MLS Dec 10 '13 at 13:18
  • I get the same problem trying to output ggplot2 plots with Cairo. I wonder if its a bug so have reported to Cairo devs. @shujaa why would this be a duplicate of a question that makes no mention of this specific error reported by this OP? – geotheory Dec 10 '13 at 17:19
  • Duplicate suggestion retracted. Apologies! – Gregor Thomas Dec 10 '13 at 18:17

2 Answers2

2

You're trying to create a plot that is 4 x 4 pixels small -- which won't work. Maybe you meant to use some other unit? Something like CairoPNG("overlay.png", 400, 400) may be a bit more realistic...

Simon Urbanek
  • 13,842
  • 45
  • 45
0

Not entirely sure what is going on here. In my case, I do NOT get any errors with the sample code you provide but I get an empty file (a syntactically correct png, but it just doesn't have anything in it).

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
[1] Cairo_1.5-2     ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       grid_3.0.1        
 [5] gtable_0.1.2       labeling_0.2       MASS_7.3-29        munsell_0.4.2     
 [9] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5 reshape2_1.2.2    
[13] scales_0.2.3       stringr_0.6.2      tools_3.0.1      

I do note a difference when I use

CairoPNG(...) #pseudo code
g
dev.off()

compared to

CairoPNG(...) #id
print(g)
dev.off()

the former says Cairo when the device is closed whereas the latter says windows. Yet, based on this question, the latter should be the correct way to print a plot under ggplot.

Odd!

Community
  • 1
  • 1
Paul Lemmens
  • 595
  • 5
  • 14