0

I was trying to use alpha conversion to col argument in plot function. How do I do that without having to write col=alpha(each_color,.5) for each_color.

Is there a way to vectorize that?

Here is reproducible example:

set.seed(10)
mydata <- rnorm(100)
mycol <-c("#FDF6E3", "#B3E2CD", "#FDCDAC", "#CBD5E8", "#F4CAE4", "#E6F5C9", "#FFF2AE", "#F1E2CC", "#CCCCCC")
par(bg='gray30')
plot(mydata,col=mycol,pch=19,cex=3)
mycol_alpha <- paste0('alpha(\'',mycol,'\',.5)')
par(bg='white')

Is there a way to apply mycol_alpha to plot function directly or by some other way?

update:

This one has the solution. My mistake, I thought the alpha function is in base R. We need library(scales)

Solution:

set.seed(10)
mydata <- rnorm(100)
mycol <-c("#FDF6E3", "#B3E2CD", "#FDCDAC", "#CBD5E8", "#F4CAE4", "#E6F5C9", "#FFF2AE", "#F1E2CC", "#CCCCCC")
par(bg='gray30')
plot(mydata,col=scales::alpha(mycol,.5),pch=19,cex=3)
Community
  • 1
  • 1
Stat-R
  • 5,040
  • 8
  • 42
  • 68
  • I don't believe so, but you did misspell `alpha` as `aphla`... Do you still see the transparency? Also, your use of `paste0` already *is* vectorized. – Robert Krzyzanowski Mar 24 '14 at 15:07
  • I’m not entirely sure what the question is. I’m assuming `alpha` is a custom function. Is it not vectorised? Why not? Can it be vectorised by writing `Vectorize(alpha)`? – Konrad Rudolph Mar 24 '14 at 15:07
  • 2
    try `grDevices::adjustcolor` for a base solution – baptiste Mar 24 '14 at 15:43
  • The base solution is most general, but `scales::alpha` is already vectorized (if that's the `alpha()` function you're using). With that function, you don't need the paste, just `mycol_alpha <- alpha(my_col, 0.5)`. Or `mycol_alpha <- adjustcolor(my_col, alpha.f = 0.5)`. – Gregor Thomas Mar 25 '14 at 18:34

1 Answers1

1

vectorization is not your problem as noted by @Robert.

add

transparency<-round(255*0.5) # 50% alpha

and try

mycol_alpha <- paste0(mycol,as.hexmode(transparency))

that should add transparency in plot( ... , col=mycol_alpha , ...)

no need for scales::alpha.

Janhoo
  • 597
  • 5
  • 21