Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semi-transparency in RStudio

I'm trying to create a plot with a semi-transparent confidence region around the regression line:

library(car)
library(ggplot2)
library(effects)

mod <- lm(salary~yrs.service+sex, data=Salaries)

yrseff <- as.data.frame(allEffects(mod)[[1]])

ggplot(yrseff, aes(x=yrs.service, y=fit))+
geom_ribbon(aes(ymin=lower, ymax=upper), alpha=.2)+
geom_line(colour="darkgreen", size=2)

I get this error message:

Warning message: In grid.Call.graphics(L_polygon, x$x, x$y, index) : semi-transparency is not supported on this device: reported only once per page

However, if I first open a pdf device (as in code below) it creates a pdf file with the semi-transparent ribbon.

pdf()
ggplot(yrseff, aes(x=yrs.service, y=fit))+
geom_ribbon(aes(ymin=lower, ymax=upper), alpha=.2)+
geom_line(colour="darkgreen", size=2)
dev.off()

What might be the problem? Is there a way to obtain semi-transparency without having to save to a pdf?

I'm using RStudio on Ubuntu 12.04 and here is my session info.

> sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: i686-pc-linux-gnu (32-bit)

locale:
[1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
[4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
[7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] ggplot2_0.9.3.1  car_2.0-19       effects_3.0-0    colorspace_1.2-4
[5] lattice_0.20-27 

loaded via a namespace (and not attached):
[1] dichromat_2.0-0    digest_0.6.4       gtable_0.1.2       labeling_0.2      
[5] MASS_7.3-29        munsell_0.4.2      nnet_7.3-7         plyr_1.8.1        
[9] proto_0.3-10       RColorBrewer_1.0-5 Rcpp_0.11.1        reshape2_1.2.2    
[13] scales_0.2.3       stringr_0.6.2      tools_3.0.3 

And, in case this is useful information:

getOption("device")
[1] "RStudioGD"
like image 712
msoftrain Avatar asked Mar 22 '14 18:03

msoftrain


2 Answers

per request of the OP:

Can you paste the output of getOption("bitmapType") in your config? If it's not "cairo" try setting it to that via options(bitmapType="cairo") and see if you get the same error.

like image 89
hrbrmstr Avatar answered Sep 18 '22 14:09

hrbrmstr


I had exactly the same problem as the OP, but in my case setting options(bitmapType="cairo") did not solve the problem.

In my case the problem was caused by the fact that I compiled R manually from source without the --with-cairo configure option (or rather: my system was lacking the necessary libcairo2-dev package, the --with-cairo did not have any effect). Recompiling R with proper cairo support fixed the issue. It now even works although getOption("bitmapType") is still set to `"Xlib".

like image 28
bluenote10 Avatar answered Sep 17 '22 14:09

bluenote10