Here is the code for “colorRampPaletteAlpha”, which can be helpful when creating a color palette. The two numbers after the # in the capture code indicate transparency.
・GitHub「colorRampPaletteAlpha」
https://github.com/mylesmharrison/colorRampPaletteAlpha
Checked with R version 4.2.2.
Example
See the command and package help for details.
#from Github source
install.packages("devtools")
devtools::source_url("https://raw.githubusercontent.com/mylesmharrison/colorRampPaletteAlpha/master/colorRampPaletteAlpha.R")
#Loading the scales package
library("scales")
#Get color code for specified transparency and color:addalpha command
#Alpha value:alpha option
show_col(addalpha("red", alpha = 0.1))
###code#####
function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
#Create a gradient palette with the specified color:colorRampPaletteAlpha command
#Specify completion method:interpolate option;"spline","linear"
show_col(colorRampPaletteAlpha(c("red", "yellow", "blue"), n = 20, interpolate = "spline"))
###code#####
function(colors, n=32, interpolate='linear') {
# Create the color ramp normally
cr <- colorRampPalette(colors, interpolate=interpolate)(n)
# Find the alpha channel
a <- col2rgb(colors, alpha=T)[4,]
# Interpolate
if (interpolate=='linear') {
l <- approx(a, n=n)
} else {
l <- spline(a, n=n)
}
l$y[l$y < 255] <- 255 # Clamp if spline is < 255
cr <- addalpha(cr, l$y/255.0)
return(cr)
}
Output Example
・addalpha command

・colorRampPaletteAlpha command:”spline”

・colorRampPaletteAlpha command:”linear”

I hope this makes your analysis a little easier !!