Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot confidence region shading with graded alpha (transparency) level

Tags:

plot

r

I would like to plot shaded confidence regions for various lines, but would like the alpha level in these regions to vary gradually from b to c, where b is the alpha at the median, and c is the alpha at whatever outer quantile I am using. The following code generates a line and confidence region plot as I would like, but without the variable transparency.

x= 1:10+rnorm(10)
xhigh=x+rnorm(10)^2
xlow=x-rnorm(10)^2

plot(x,type='l')
polygon(x=c(1:length(xlow),length(xlow):1),   y=c(xhigh,xlow[length(xlow):1]),col = rgb(1,0,0,.1),border=NA)
like image 593
Charlie Avatar asked Jan 24 '17 13:01

Charlie


1 Answers

You can overplot many polygons:

plot(x,type='l')
for (i in seq(0, 1, 0.01)) {
  polygon(x = c(x + i * (xhigh - x), x - i * (xlow - x)), 
          col = rgb(1, 0, 0, .005), border = NA)
}

enter image description here

Altough, I think your example is actually wrong, and probably want something like:

plot(x,type='l')
for (i in seq(0, 1, 0.01)) {
  polygon(x = c(1:10, 10:1),
          y = c(x + i * (xhigh - x), rev(x - i * abs(x - xlow))), 
          col = rgb(1, 0, 0, .005), border = NA)
}

enter image description here

like image 132
Axeman Avatar answered Nov 20 '22 21:11

Axeman