我正在使用smoothScatter
绘制一些数据。我正在尝试将密度的颜色从默认的colorRampPalette(c("white", blues9)
更改为透明红色。我怎么能这样做?
我试过了:
smoothScatter(
x,y,nrpoints=length(df$x),
colramp = colorRampPalette(c(rgb(1,1,1,0.5),rgb(1,0,0,0.5))
)
但这不起作用。
更一般地说,我如何创建一个colorRampPalette
函数,可以作为一个函数传入,包括使所有颜色透明?
答案 0 :(得分:8)
您只需在致电alpha=TRUE
时添加colorRampPalette
:
>?colorRampPalette
alpha: logical: should alpha channel (opacity) values should be
returned? It is an error to give a true value if ‘space’ is
specified.
> colorRampPalette(c(rgb(1,1,1,0.5),rgb(1,0,0,0.5)))(3)
[1] "#FFFFFF" "#FF7F7F" "#FF0000"
> colorRampPalette(c(rgb(1,1,1,0.5),rgb(1,0,0,0.5)), alpha=TRUE)(3)
[1] "#FFFFFF80" "#FF7F7F80" "#FF000080"
由于您的colorRampPalette
似乎没有alpha
参数,因此这是一个手动解决方案:
# Specify alpha as a percentage:
colorRampAlpha <- function(..., n, alpha) {
colors <- colorRampPalette(...)(n)
paste(colors, sprintf("%x", ceiling(255*alpha)), sep="")
}
colorRampAlpha(c(rgb(1,1,1),rgb(1,0,0)), n=3, alpha=0.5)
[1] "#FFFFFF80" "#FF7F7F80" "#FF000080"
但是,您需要事先指定所需的颜色数量。
答案 1 :(得分:2)
这是使用colorRampPalette
创建透明调色板的不雅方式## first specify your color scheme
colorRampPalette(c("lightgrey","grey","lightblue","blue","darkblue")) -> blues
## Then create the palette as you would normally
blues_pal<-blues(500)
## Finally use paste to set transparency to every color in the vector
blues_pal_transp<-paste(blues_pal, "50", sep="")
## and an example
image(volcano, col=blues_pal_transp)