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
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
Tena koe Ralf If you save the results of density() x1Den <- density(x1) you get the x and y values of the line which is plotted. Similarly for x2 - you can then use these to shade the joint area and find the area. Tinkering with the arguments of density to make the x values for each the same will make this process easier. Let me know if you'd like more details. HTH .... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Ralf B > Sent: Thursday, 23 September 2010 8:55 a.m. > To: r-help Mailing List > Subject: [R] Determine area between two density plots > > 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 > > ______________________________________________ > 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.The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.