Nguyen Dinh Nguyen
2007-Mar-13 04:19 UTC
[R] Highlight overlapping area between two curves
Dear R helpers, I have a graph as following; I would like to highlight the overlapping area between the two curves. Do you know how to do this? Thank you in advance for your help. Nguyen ###START x1 <- rnorm(10000, 0.70,0.12) x2 <- rnorm(10000, 0.90,0.12) d1 <- density(x1) d2 <- density(x2) plot(range(d1$x,d2$x), range(d1$y, d2$y), type = "n", xlab = "X value", ylab = "Probability Density" ) lines(d1, col = "red",lwd=4, lty=2) lines(d2, col = "blue",lwd=4) ##END CODE
See http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=7 and code therein. -Christos> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Nguyen > Dinh Nguyen > Sent: Tuesday, March 13, 2007 12:20 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Highlight overlapping area between two curves > > Dear R helpers, > I have a graph as following; I would like to highlight the > overlapping area between the two curves. Do you know how to do this? > Thank you in advance for your help. > Nguyen > > ###START > x1 <- rnorm(10000, 0.70,0.12) > x2 <- rnorm(10000, 0.90,0.12) > > d1 <- density(x1) > d2 <- density(x2) > > plot(range(d1$x,d2$x), range(d1$y, d2$y), type = "n", > xlab = "X value", ylab = "Probability Density" ) > > lines(d1, col = "red",lwd=4, lty=2) > lines(d2, col = "blue",lwd=4) > > ##END CODE > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > >
On Mar 13, 2007, at 12:19 AM, Nguyen Dinh Nguyen wrote:> Dear R helpers, > I have a graph as following; I would like to highlight the > overlapping area > between the two curves. Do you know how to do this? > Thank you in advance for your help.Perhaps not exactly what you wanted, but it might give you some ideas: p <- seq(0.2,1.4,0.01) x1 <- dnorm(p, 0.70, 0.12) x2 <- dnorm(p, 0.90, 0.12) plot(range(p), range(x1,x2), type="n") lines(p, x1, col = "red",lwd=4, lty=2) lines(p, x2, col = "blue",lwd=4) polygon(c(p,p[1]),c(pmin(x1,x2),0), col="grey")> NguyenHaris Skiadas Department of Mathematics and Computer Science Hanover College
Bojanowski, M.J. (Michal)
2007-Mar-13 10:49 UTC
[R] Highlight overlapping area between two curves
If PDF is OK, you can use the 'alpha' argument in colors, i.e.: pdf( "file.pdf") p <- seq(0.2,1.4,0.01) x1 <- dnorm(p, 0.70, 0.12) x2 <- dnorm(p, 0.90, 0.12) plot(range(p), range(x1,x2), type="n") polygon(p, x1, col = rgb(1,0,0, .5),lwd=4, lty=2) polygon(p, x2, col = rgb(0,0,1, .5),lwd=4) dev.off() hth, Michal *** Note that my e-mail address has changed to m.j.bojanowski at uu.nl *** Please update your address books accordingly. Thank you! _________________________________________ Michal Bojanowski ICS / Sociology Utrecht University Heidelberglaan 2; 3584 CS Utrecht Room 1428 m.j.bojanowski at uu.nl http://www.fss.uu.nl/soc/bojanowski -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Nguyen Dinh Nguyen Sent: Tuesday, March 13, 2007 5:20 AM To: r-help at stat.math.ethz.ch Subject: [R] Highlight overlapping area between two curves Dear R helpers, I have a graph as following; I would like to highlight the overlapping area between the two curves. Do you know how to do this? Thank you in advance for your help. Nguyen ###START x1 <- rnorm(10000, 0.70,0.12) x2 <- rnorm(10000, 0.90,0.12) d1 <- density(x1) d2 <- density(x2) plot(range(d1$x,d2$x), range(d1$y, d2$y), type = "n", xlab = "X value", ylab = "Probability Density" ) lines(d1, col = "red",lwd=4, lty=2) lines(d2, col = "blue",lwd=4) ##END CODE ______________________________________________ R-help at stat.math.ethz.ch 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.