Search code examples
rplotkernel-density

color overlapping lines in plot based on density


I've got a large dataset of a time series that I'd like to plot with lines indicating values across the time points. Example:

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

I would like to do something analogous to smoothScatter to illustrate the density of the overlap of lines across the time points (e.g. where multiple lines overlap, the colors will change from black to red), but have been unable to make anything work to that effect.

Does anyone know of a function/package than can do this or something similar?

Thanks in advance!


Solution

  • I think you can get such effects by overlapping alpha red to black. (But I feel the output one alpha color makes is cool)

    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))
    

    enter image description here enter image description here