I have the following:
set.seed(513)
x1 = rnorm(12^6, 5,5)
x2 = x1-10
plot(density(x1),xlim = c(-25,25), ylim = c(0,0.09), xaxs = "i", yaxs = "i", main ="", xaxt = "n", yaxt = "n", xlab = "", ylab = "", col = "blue")
lines(density(x2), col = "red")
axis(side = 1, pos = 0, at = c(-30,-20,-10,0,10,20,30), labels = c("",-2,-1,0,+1,+2,""), tck = -0.02)
lines(x=c(0,0),y=c(0,1), lty = 3)
polygon(density(x1[x1>=0]), col = rgb(0,0,0.5,0.5))
polygon(density(x2[(x2)>=0]), col = rgb(0.5,0,0,0.5))
par(xpd=TRUE)
text("Frequency", x = -29, y = 0.045, srt = 90)
par(xpd=FALSE)
I would like to make a plot which shows two normal curves, with the space where X>0 having filled colours. I tried using polygon
but it seems to struggle with density (it seems using x1[x1>=0]
limits the density function to that data, I think I need it to make the distribution first and then cut it)
I would like to fill the space under the curves for both lines where X is zero or larger.
I managed to make a rather inelegant solution...