[R] exactly overlaid semi-transparent lines

Daniel Farewell farewelld at Cardiff.ac.uk
Thu Jul 23 15:57:42 CEST 2009


I'd like to plot n (say n = 10) semi-transparent lines in such a way that if all n happen to exactly overlay each other, the resulting line is completely opaque. In principle, I believe that this is what setting alpha = 1/n should accomplish.

In practice, different values of n produce different results. Consider the following function, which overlays n semi-transparent red lines:

mylines <- function(n) {

 replicate(n, lines(0:1, rep(n, 2), col = rgb(1, 0, 0, alpha = 1/n)))

}

Opening up a device that supports translucency (such as pdf) and plotting the resulting lines for n = 1, ..., 256:

pdf("alpha.pdf")
plot(0:1, c(1, 256), type = "n")
invisible(lapply(1:256, mylines))
dev.off()

Unfortunately, none of the lines for n > 1 look quite like the line for n = 1. The PDF looks quite different on screen depending on how much I zoom in, and my (reasonably modern) printer just stops printing anything above about n = 170.

I see that there has been quite a bit of discussion on R-help about the subtleties of semi-transparency, but I wonder if anyone can suggest a way that I might achieve what I'm after? (The application I have in mind is multiple imputation, plotting n imputations at alpha = 1/n to see where there is, or is not, variability.)

Many thanks,

Daniel Farewell
Cardiff University

R version 2.9.1 (2009-06-26) 
i386-apple-darwin8.11.1 

locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8

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

loaded via a namespace (and not attached):
[1] tools_2.9.1




More information about the R-help mailing list