[R] Determine area between two density plots

Ralf B ralf.bierig at gmail.com
Wed Sep 22 22:54:46 CEST 2010


Hi group,

I am creating two density plots as shown in the code below:

x1 <- c(1,4,5,3,2,3,4,5,6,5,4,3,2,1,1,1,2,3)
x2 <- c(1,4,5,3,5,7,4,5,6,1,1,1,2,1,1,1,2,3)
plot(density(x1, na.rm = TRUE))
polygon(density(x2, na.rm = TRUE), border="blue")

How can I determine the area that is covered between the two plots as
a number and how can I grey (or highlight with a pattern) the area
that lies between the two lines?

Thanks,
Ralf



More information about the R-help mailing list