基于密度的绘图中的颜色重叠线

color overlapping lines in plot based on density

我有一个时间序列的大型数据集,我想用指示跨时间点的值的线来绘制它。示例:

df.test=data.frame(runif(1000),runif(1000),runif(1000)*2,runif(1000)/2)
matplot(t(df.test),type="l",col="black")

我想做一些类似于smoothScatter的事情来说明跨时间点的线条重叠的密度(例如,多条线条重叠的地方,颜色将从黑色变为红色),但是有无法使任何事情达到那种效果。

有人知道 function/package 可以做这个或类似的事情吗?

提前致谢!

我认为您可以通过将 alpha 红色重叠到黑色来获得这种效果。 (但我觉得一个alpha颜色的输出很酷)

set.seed(1); df.test = data.frame(runif(1000), runif(1000), runif(1000)*2, runif(1000)/2)

matplot(t(df.test), type = "l", col = "black")
matplot(t(df.test), type = "l", col = ggplot2::alpha("red", 0.1), add=T)

matplot(t(df.test), type = "l", col = ggplot2::alpha("black", 0.2))