[R] Determine area between two density plots

David Winsemius dwinsemius at comcast.net
Wed Sep 22 23:09:58 CEST 2010


On Sep 22, 2010, at 4:54 PM, Ralf B wrote:

> 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?

Have you tried searching on one of the available search sites for:

area between two density curves

.... ?  (quite a few hits on Baron's site, almost a FAQ)

Calculating area would require setting the limits so they line up and  
integrating either the difference in their values (or their absolute  
values depending on what the question really was) over their range:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/119793.html

-- 
David.
>
> Thanks,
> Ralf
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list