r - 查找密度图中前 10% 的截断点

标签 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

最佳答案

我认为您正在寻找 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

关于r - 查找密度图中前 10% 的截断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111947/

相关文章:

r - 使用矩阵从因子中获取值

c++ - 通过算法生成阴影?

opengl - OpenGL 中的广告牌效果

java - 我正在寻找 Java 图形库

r - 用增量序列号填充数据框列

r - 抖动 geom_line()

r - 在 POSIXct 上附加日期时间对象

Java repaint() 在 jPanel 类中不起作用

c++ - 无窗口栏 SDL C++ 应用程序

r - 在 R 中对不同的十六进制颜色进行排序的最佳实践