找到密度图中前10%的截断点

时间:2011-11-13 13:43:43

标签: r graphics

我想遮蔽前10%本地化的区域。我只是随意把截断点65,只是绘制这个情节。这就是我想要找到的......每个数据集。

xf <- rnorm(40000, 50, 10);
plot(density(xf),xlim=c(0,100), main = paste(names(xf), "distribution"))
dens <- density(xf)
x1 <- min(which(dens$x >= 65)) # I want identify this point such that 
# the shaded region includes top 10%

x2 <- max(which(dens$x <  max(dens$x)))
with(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col="green"))
abline(v= mean(traitF2),  col = "black", lty = 1, lwd =2)

enter image description here

2 个答案:

答案 0 :(得分:10)

我认为您正在寻找quantile()功能:

xf <- rnorm(40000, 50, 10)
plot(density(xf),xlim=c(0,100), main = paste(names(xf), "distribution"))
dens <- density(xf)
x1 <- min(which(dens$x >= quantile(xf, .90))) # quantile() ftw!

x2 <- max(which(dens$x <  max(dens$x)))
with(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col="green"))

enter image description here

答案 1 :(得分:0)

您正在寻找分位数。 http://en.wikipedia.org/wiki/Quantile