Tuesday, May 12, 2015

How to change the alpha value of colours in R

How to change the alpha value of colours in R

Often I like to reduce the alpha value (level of transparency) of colours to identify patterns of over-plotting when displaying lots of data points with R. So, here is a tiny function that allows me to add an alpha value to a given vector of colours, e.g. a RColorBrewer palette, using col2rgb and rgb, which has an argument for alpha, in combination with the wonderful apply and sapply functions.

1 2 3 4 5 6 7 8
## Add an alpha value to a colour
add.alpha <- function(col, alpha=1){
if(missing(col))
stop("Please provide a vector of colours.")
apply(sapply(col, col2rgb)/255, 2,
function(x)
rgb(x[1], x[2], x[3], alpha=alpha))
}
view raw add.alpha.R hosted with ❤ by GitHub

The example below illustrates how this function can be used with colours provided in different formats, thanks to the col2rgb function.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# Source add.alpha function from Github
require(RCurl)
source(textConnection(getURL("https://gist.github.com/mages/5339689/raw/576263b8f0550125b61f4ddba127f5aa00fa2014/add.alpha.R")))
 
## Example
set.seed(1)
n <- 1200
dat <- data.frame(
x = gl(n=4, k=n/4),
y = rnorm(n)
)
 
myColours = c(1, "steelblue", "#FFBB00", rgb(0.4, 0.2, 0.3))
myColoursAlpha <- add.alpha(myColours, alpha=0.4)
## "#00000066" "#4682B466" "#FFBB0066" "#66334D66"
 
op <- par(mfrow=c(1,2), mar=c(2,2,3,1))
boxplot(y ~ x, data=dat, outline=FALSE,
axes=FALSE, main="alpha=1")
points(x=jitter(as.numeric(dat$x)), y=dat$y,
col=myColours[dat$x], pch=19)
box()
 
boxplot(y ~ x, data=dat, outline=FALSE,
axes=FALSE, main="alpha=0.4")
points(x=jitter(as.numeric(dat$x)), y=dat$y,
col=myColoursAlpha[dat$x], pch=19)
box()
par(op)
view raw add.alpha.example.R hosted with ❤ by GitHub

Session Info

sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

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

other attached packages:
[1] RCurl_1.95-4.1 bitops_1.0-5

No comments:

Post a Comment