如何持续改变透明度?

问题描述

假设我们在 R 中创建了一个彩色带:

library(ggplot2)
dev.new(width=6,height=3)
ggplot(data.frame(20,20)) +
  geom_rect(aes(xmin=0,xmax=100,ymin=0,ymax=50),fill="blue")

enter image description here

我想用正态分布的 alpha 值沿 y 轴连续改变波段的透明度:

dnorm(y - 25) / 12.5) / dnorm(0)

如何实现?谢谢!

解决方法

您可以将其绘制为一堆离散矩形,其中 alpha 随 y 变化,具体取决于您想要的函数。

library(ggplot2)
# make this bigger for smaller rects/smoother gradient
n_rects <- 51

dat <- data.frame(y=seq(0,50,length.out = n_rects))
dat$alpha <- dnorm((dat$y - 25) / 12.5) / dnorm(0)
ggplot(dat) +
  geom_rect(xmin=0,xmax=100,aes(ymin=y,ymax=y+1,alpha=alpha),fill="blue")

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...