[R] highlight overlapping region of two densities

2013-03-20 Thread andrija djurovic
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.


Re: [R] highlight overlapping region of two densities

2013-03-20 Thread Pierrick Bruneau
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 djandr...@gmail.comwrote:

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

__
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.


Re: [R] highlight overlapping region of two densities

2013-03-20 Thread andrija djurovic
Hi Pierrick,

thanks for reply. This is the closest solution that I got.

I also found something like this:

dens1 - density(myd)
dens2 - density(myd1)
plot(range(dens1$x, dens2$x), range(dens1$y, dens2$y), type = n)
polygon(dens1$x, dens1$y, col = rgb(1,0,0, .5),lwd=4, lty=2)
polygon(dens2$x, dens2$y, col = rgb(0,0,1, .5),lwd=4)


Your solution is closer to what I want but also solution above can be,
maybe,  useful.

Thank you

Andrija

On Wed, Mar 20, 2013 at 2:20 PM, Pierrick Bruneau pbrun...@gmail.comwrote:

 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 djandr...@gmail.comwrote:

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

__
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.


Re: [R] highlight overlapping region of two densities

2013-03-20 Thread Marc Girondot

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.giron...@u-psud.fr
Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html
Skype: girondot

__
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.


Re: [R] highlight overlapping region of two densities

2013-03-20 Thread Rui Barradas

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@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.



__
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.


Re: [R] highlight overlapping region of two densities

2013-03-20 Thread andrija djurovic
Hi Rui.

Thank you very much. I had similar idea like yours but your is more elegant.

Andrija


On Wed, Mar 20, 2013 at 4:03 PM, Rui Barradas ruipbarra...@sapo.pt wrote:

 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@r-project.org mailing list
 https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/**
 posting-guide.html http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



[[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.