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

