0

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?

Jonathan1234
  • 489
  • 2
  • 9
  • 1
    You need the line to be transparent - use the alpha parameter - zero for completely transparent (i.e. invisible), 1 for opaque. If you are overplotting 1000 times you will need alpha quite small. See here for a way of doing it... https://stackoverflow.com/questions/12995683/any-way-to-make-plot-points-in-scatterplot-more-transparent-in-r – Andrew Gustar May 18 '22 at 10:45

1 Answers1

2
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)
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22