[R] latex, eps graphics and transparent colors

Ben Bolker bbolker at gmail.com
Wed Apr 13 21:57:16 CEST 2011


Michael Friendly <friendly <at> yorku.ca> writes:

> 
> I have a diagram to be included in latex, where all my figures are .eps 
> graphics (so pdflatex is not an
> option) and I want to achieve something
> like the following: three concentric filled circles varying in lightness 
> or saturation.  It is easiest to do this using
> transparency, but in my test using the postscript driver, the 
> transparent color fills do not appear.  Is it
> correct that postscript() does not support transparency?
> 
> circle <-function (radius = 1, segments=61) {
>          angles <- (0:segments)*2*pi/segments
>          radius * cbind( cos(angles), sin(angles))
> }
> 
> plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='',
>          asp=1, xaxt="n", yaxt="n")
> 
> #clrs <- trans.colors("lightblue", alpha=c(.2, .4, .6))  ## from heplots 
> package
> clrs <- c("#ADD8E633", "#ADD8E666", "#ADD8E699")
> 
> c1 <- circle(3)
> polygon(    c1, col=clrs[1], border="lightblue")
> polygon(.67*c1, col=clrs[2], border="lightblue")
> polygon(.33*c1, col=clrs[3], border="lightblue")
> 
> arrows(-1,  0, 5, 0, angle=10, length=.2, lwd=2, col="darkgray")
> arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col="darkgray")
> 
> One alternative that sort of works is to use the png() driver, and then
> convert fig.png fig.eps
> but I need very high resolution to make the real diagram legible.
> 
> It might suffice to use hcl() colors to approximate what I've done with 
> transparency,
> but I don't know how to start with a given color ("lightblue") and 
> achieve roughly
> similar resuts.
> 

  If you really only want to lighten a specified colour (rather
than overlaying multiple colours), then something like this ought
to do the trick:


testfun <- function(clrs=c("#ADD8E633", "#ADD8E666", "#ADD8E699")) {
  plot(1:5, 1:5, type='n', xlim=c(-1,5), ylim=c(-1,5), xlab='', ylab='',
       asp=1, xaxt="n", yaxt="n")
  c1 <- circle(3)
  polygon(    c1, col=clrs[1], border="lightblue")
  polygon(.67*c1, col=clrs[2], border="lightblue")
  polygon(.33*c1, col=clrs[3], border="lightblue")

  arrows(-1,  0, 5, 0, angle=10, length=.2, lwd=2, col="darkgray")
  arrows( 0, -1, 0, 5, angle=10, length=.2, lwd=2, col="darkgray")
}

postscript("testalpha1.ps")
testfun()
dev.off()


lblue <- "#ADD8E6"
alphafy <- function(col,alpha=1) {
  rr <- 1-alpha*(1-c(col2rgb(col)/255))
  rgb(rr[1],rr[2],rr[3])
}
alphafy("#ADD8E6")
alphafy("#ADD8E6",alpha=0)

postscript("testalpha2.ps")
testfun(clrs=c(alphafy(lblue,0.2),alphafy(lblue,0.4),alphafy(lblue,0.6)))
dev.off()



More information about the R-help mailing list