[R] highlight overlapping region of two densities
Marc Girondot
marc_grt at yahoo.fr
Wed Mar 20 15:43:48 CET 2013
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
More information about the R-help
mailing list