Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shading a kernel density plot between two points.

Tags:

plot

r

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 <- density(draws) plot(dens) #or in one line like this: plot(density(rnorm(100)^2)) 

Which gives me this nice little PDF:

enter image description here

I'd like to shade the area under the PDF from the 75th to 95th percentiles. It's easy to calculate the points using the quantile function:

q75 <- quantile(draws, .75) q95 <- quantile(draws, .95) 

But how do I shade the the area between q75 and q95?

like image 686
JD Long Avatar asked Aug 16 '10 15:08

JD Long


People also ask

How do you interpret the Y axis of a kernel density plot?

The vertical or y-axis of a KDE plot represents the Kernel Density Estimate of the Probability Density Function of a random variable, which is interpreted as a probability differential. The probability of a value being between the points x1 and x2 is the total shaded area of the curve under the two points.

What does a kernel density plot tell you?

A density plot is a representation of the distribution of a numeric variable. It uses a kernel density estimate to show the probability density function of the variable (see more). It is a smoothed version of the histogram and is used in the same concept.


2 Answers

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)

enter image description here

like image 123
Dirk Eddelbuettel Avatar answered Sep 18 '22 00:09

Dirk Eddelbuettel


Another solution:

dd <- with(dens,data.frame(x,y))  library(ggplot2)  qplot(x,y,data=dd,geom="line")+   geom_ribbon(data=subset(dd,x>q75 & x<q95),aes(ymax=y),ymin=0,               fill="red",colour=NA,alpha=0.5) 

Result:

alt text

like image 33
Ben Bolker Avatar answered Sep 22 '22 00:09

Ben Bolker