col2rgb
and rgb
, which has an argument for alpha
, in combination with the wonderful apply
and sapply
functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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)) | |
} |
The example below illustrates how this function can be used with colours provided in different formats, thanks to the
col2rgb
function. Read more »