[R] Selectively shading areas under two density curves
Ian Watson
ian.watson at mq.edu.au
Sun Aug 5 03:40:12 CEST 2007
Dear Listers,
I am drawing a plot of two density curves, for male and female incomes. I would
like to shade/hatch/color (whatever) the areas under the curves which are
distinctive for each gender. This is the code I have tried so far:
m <- density(topmal.d$y, bw = "sj")
f <- density(topfem.d$y, bw = "sj")
par(mfrow = c(1,1))
plot(x = c(0,400), y = c(0,0.02), type = "n",
bty = "l", xlab = "Annual earnings (in $thousands)", ylab = "Density")
polygon(m, col = "blue", border = "blue", density = 20, angle = -45)
polygon(f, col = "red", border = "red", density = 20, angle = 45)
Without the data I realise you may not be able to see my goal. But basically the
m density and the f density overlap for most of the plot, but areas "bulge"
out on the left and right of the overlapped area. What I'm wanting to do is
shade/hatch/color etc these areas which are unique to each density, that is,
which are outside the overlapped area.
My code is successful at hatching the bulging areas, but leaves me with a double
hatched area for the overlap, which is distracting. If I could turn this double
hatched area white, that would achieve my goal, though ideally I would like to
be able to specify something like "shade only areas under the m density curve
which are not also under the f density curve (and shade only under the f density
curve those areas not under the m density curve".
Any advice would be most appreciated.
I am using R 2.5.1. under Windows XP.
Kind regards
Ian Watson
More information about the R-help
mailing list