ggplot2:为alpha设置(非线性)值

die*_*ne. 5 alpha r ggplot2

我想绘制一个镜像的95%密度曲线并将alpha映射到密度:

foo <- function(mw, sd, lower, upper) {
x <- seq(lower, upper, length=500)
dens <- dnorm(x, mean=mw, sd=sd, log=TRUE)
dens0 <- dens -min(dens)
return(data.frame(dens0, x))
}

df.rain <- foo(0,1,-1,1)

library(ggplot2)


drf <- ggplot(df.rain, aes(x=x, y=dens0))+
geom_line(aes(alpha=..y..))+
geom_line(aes(x=x, y=-dens0, alpha=-..y..))+
stat_identity(geom="segment", aes(xend=x, yend=0, alpha=..y..))+
stat_identity(geom="segment", aes(x=x, y=-dens0, xend=x, yend=0, alpha=-..y..))
drf
Run Code Online (Sandbox Code Playgroud)

这很好,但我想让边缘和中间的对比更加突出,即我希望边缘几乎是白色而只有中间部分是黑色的.我一直在篡改,scale_alpha()但没有运气.有任何想法吗?

编辑:最后,我想绘制几个雨滴,即单个滴点会很小,但阴影应该仍然清晰可见.

die*_*ne. 0

在思考你的答案时,我实际上找到了我正在寻找的东西。最简单的方法是简单地使用scale_colour_gradientn灰色向量。

library(RColorBrewer)
grey <- brewer.pal(9,"Greys")

drf <- ggplot(df.rain, aes(x=x, y=dens0, col=dens0))+
 stat_identity(geom="segment", aes(xend=x, yend=0))+
 stat_identity(geom="segment", aes(x=x, y=-dens0, xend=x, yend=0))+
 scale_colour_gradientn(colours=grey)
drf
Run Code Online (Sandbox Code Playgroud)