Skip to content

Instantly share code, notes, and snippets.

@mages
Last active December 20, 2018 14:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mages/5339689 to your computer and use it in GitHub Desktop.
Save mages/5339689 to your computer and use it in GitHub Desktop.
How to change the alpha value of colours in R
## 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))
}
@danlewer
Copy link

rgb and col2rgb are both vectorized. Perhaps:

add.alpha <- function(cols, alpha) rgb(t(col2rgb(cols)/255), alpha = alpha)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment