Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find truncation point for top 10% in density plot

Tags:

r

graphics

I want to shade the region where top 10% are localized. I just arbitrary put truncation point 65, to just plot this plot. That is what I intend to find...for every data sets.

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

like image 990
jon Avatar asked Dec 27 '22 11:12

jon


1 Answers

I think you are looking for the quantile() function:

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

like image 108
JD Long Avatar answered Jan 10 '23 16:01

JD Long