Hi all. I would like to highlight overlapping regions of two densities and I could not find a way to do it. Here is the sample code: myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3) myd1 <- myd-2 plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, density(myd1)$y), type = "n") lines(density(myd), col=1, lwd=4) lines(density(myd1), col=2, lwd=4, lty=2) So, I am trying to highlight the region from 1 to 4, on x axis, taking the minimum value of corresponding y values. I am aware of polygon function but don't know how to define correctly x and y coordinate vectors for this function. Could someone help me to solve this? Thank in advance. Andrija [[alternative HTML version deleted]]
Hi Andrija, As far as I understood, your problem comes from the density() function - the domains of myd and myd1 are different, which causes slightly shifted x values in the density() output. A simple workaround (which you maybe already came up with): dens1 <- density(myd, from=-1, to=6) # force both densities on same domains, dens2 <- density(myd1, from=-1, to=6) # which facilitates drawing plot(range(dens1$x, dens2$x), range(dens1$y, dens2$y), type = "n") lines(dens1, col=1, lwd=4) lines(dens2, col=2, lwd=4, lty=2) polygon(dens1$x, pmin(dens1$y, dens2$y), col="grey") But doing so causes inelegant distribution tails... Maybe somebody will have a more elegant suggestion ? Pierrick Bruneau CRP Gabriel Lippmann On Wed, Mar 20, 2013 at 1:32 PM, andrija djurovic <djandrija@gmail.com>wrote:> Hi all. > > I would like to highlight overlapping regions of two densities and I could > not find a way to do it. > > Here is the sample code: > > myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, > 2, 3,3, 2.3, 3, 3, 2, 3) > myd1 <- myd-2 > plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, > density(myd1)$y), type = "n") > lines(density(myd), col=1, lwd=4) > lines(density(myd1), col=2, lwd=4, lty=2) > > So, I am trying to highlight the region from 1 to 4, on x axis, taking the > minimum value of corresponding y values. > > I am aware of polygon function but don't know how to define correctly x and > y coordinate vectors for this function. > > Could someone help me to solve this? > > Thank in advance. > > Andrija > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Le 20/03/13 13:32, andrija djurovic a ?crit :> Hi all. > > I would like to highlight overlapping regions of two densities and I could > not find a way to do it. > > Here is the sample code: > > myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, > 2, 3,3, 2.3, 3, 3, 2, 3) > myd1 <- myd-2 > plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, > density(myd1)$y), type = "n") > lines(density(myd), col=1, lwd=4) > lines(density(myd1), col=2, lwd=4, lty=2) > > So, I am trying to highlight the region from 1 to 4, on x axis, taking the > minimum value of corresponding y values. > > I am aware of polygon function but don't know how to define correctly x and > y coordinate vectors for this function. > > Could someone help me to solve this? > > Thank in advance. > > Andrija > >Here is another solution. Marc myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, 2, 3,3, 2.3, 3, 3, 2, 3) myd1 <- myd-2 plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, density(myd1)$y), type = "n") # lines(density(myd), col=1, lwd=4) # lines(density(myd1), col=2, lwd=4, lty=2) d0x <- density(myd)$x d0y <- density(myd)$y d1x <- density(myd1)$x d1y <- density(myd1)$y polygon(d0x, d0y, col="red", border=FALSE) polygon(d1x, d1y, col=rgb(0, 1, 0, 0.5), border=FALSE) -- __________________________________________________________ Marc Girondot, Pr Laboratoire Ecologie, Syst?matique et Evolution Equipe de Conservation des Populations et des Communaut?s CNRS, AgroParisTech et Universit? Paris-Sud 11 , UMR 8079 B?timent 362 91405 Orsay Cedex, France Tel: 33 1 (0)1.69.15.72.30 Fax: 33 1 (0)1.69.15.73.53 e-mail: marc.girondot at u-psud.fr Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html Skype: girondot
Hello, Try the following. d0 <- density(myd) d1 <- density(myd1) idx0 <- d0$x >= 1 & d0$x <= 4 idx1 <- d1$x >= 1 & d1$x <= 4 yy <- apply(cbind(d0$y[idx0], d1$y[idx1]), 1, min) xx <- d0$x[idx0] xx <- c(xx[1], xx, xx[1]) yy <- c(0, yy, 0) polygon(xx, yy, col = "blue") Hope this helps, Rui Barradas Em 20-03-2013 12:32, andrija djurovic escreveu:> Hi all. > > I would like to highlight overlapping regions of two densities and I could > not find a way to do it. > > Here is the sample code: > > myd <- c(2,4,5, 4,3,2,2,3,3,3,2,3,3,4,2,4,3,3,3,2,2.5, > 2, 3,3, 2.3, 3, 3, 2, 3) > myd1 <- myd-2 > plot(range(density(myd)$x, density(myd1)$x), range(density(myd)$y, > density(myd1)$y), type = "n") > lines(density(myd), col=1, lwd=4) > lines(density(myd1), col=2, lwd=4, lty=2) > > So, I am trying to highlight the region from 1 to 4, on x axis, taking the > minimum value of corresponding y values. > > I am aware of polygon function but don't know how to define correctly x and > y coordinate vectors for this function. > > Could someone help me to solve this? > > Thank in advance. > > Andrija > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >