Shading a kernel density plot between two points.

前端 未结 5 1414
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 06:59

I frequently use kernel density plots to illustrate distributions. These are easy and fast to create in R like so:

set.seed(1)
draws <- rnorm(100)^2
dens          


        
5条回答
  •  隐瞒了意图╮
    2020-11-22 07:10

    With the polygon() function, see its help page and I believe we had similar questions here too.

    You need to find the index of the quantile values to get the actual (x,y) pairs.

    Edit: Here you go:

    x1 <- min(which(dens$x >= q75))  
    x2 <- max(which(dens$x <  q95))
    with(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col="gray"))
    

    Output (added by JDL)

提交回复
热议问题