Home > Enterprise >  Colour of line in R, go from white to black, depending on how many times we draw that line
Colour of line in R, go from white to black, depending on how many times we draw that line

Time:05-19

Suppose that I create the following plot in R

plot(runif(100,0,5))

and I draw a vertical line at the point 50, and I do that 100 times

for(i in 1:100){
  abline(v = 50,col='red')
}

however, the results is exactly the same as running

abline(v = 50, col='red)

so it totally ignores that in the first case we plotted the line 100 times (so we give it more weight ideally darker red colour)

Is there a way in R to make the vertical line more thick depending on the times that we plot over it?

For example

abline(v = 50, col='red)

should correspond to a almost white red (if can say that)

for(i in 1:2){
abline(v = 50, col='red)
}

should correspond to a thicker red

for(i in 1:1000){
abline(v = 50, col='red)
}

should correspond to a dark red.

Can this even be done in R?

CodePudding user response:

plot(runif(100,0,5))

n <- 100

for(i in n:1) {
  abline(v = 50, col = adjustcolor("red", alpha = 0.01 * (n - i) / n), lwd = i)
}

enter image description here

  1. reverse your loop, as we plot line over line and start with the widest one with the lowest transparency.
  2. width of the line is set with lwd
  3. set the alpha, but you need to normalize them between 0-1 but when you have a high n you need to adjust that value to very low values to see a difference (I used 0.01 as multiplier)
  •  Tags:  
  • r
  • Related