X ~ N(mu, 3*sigma^2)
Y ~ N(mu,sigma^2)
I am trying to graphically demonstrate that P[ |X-mu| < sigma ] < P[ |Y-mu| < sigma ] (i.e., the area under the blue curve is less than the area under the red curve between 0 and sigma)
I have created the pdfs of |X-mu| and |Y-mu| with the following code where I let mu=0 and sigma=1:
x<-seq(-10,10,length=10000000)
y1<-rnorm(x,mean=0,sd=sqrt(3))
y2<-rnorm(x,mean=0,sd=1)
absy1<-abs(y1-mean(y1))
absy2<-abs(y2-mean(y2))
plot(density(absy2), type="l", axes=FALSE, xlab = "", ylab = "", main="", col="red")
lines(density(absy1), col="blue")
abline(v=1,lty=2,col="black")
text(3.5,0.18,expression(abs(N(mu,3 * sigma^2)-mu)), col="blue")
text(1.5,0.48,expression(abs(N(mu,sigma^2)-mu)), col="red")
axis(1,at=0,labels=0, line=-1)
axis(1,at=1,labels=expression(sigma), line=-1)
Here is a link to the image produced by the above code that I’m trying to use:

I want to shade the region under each curve between 0 and sigma.
To shade other, normal distribution curves, I’ve been using something like the line below, but I can’t use it with this problem (or I don’t know how).
polygon(c(0,xred,10),c(0,yred,0),col="mistyrose", border=NA)
Your help is much appreciated! Also, if there is a better way to draw the curves, please advise! (It’s my second day learning R, so please try to be very clear!) Thanks.
SOLUTION – thanks to @DWin
x<-seq(-10,10,length=10000000)
y1<-rnorm(x,mean=0,sd=sqrt(3))
y2<-rnorm(x,mean=0,sd=1)
absy1<-abs(y1-mean(y1))
absy2<-abs(y2-mean(y2))
plot(den2 <-density(absy2), type="l", axes=FALSE, xlab = "", ylab = "", main="", col="red")
with(den2, polygon(x=c(0, x[x < 1], rev(x[x<1]), 0), y=c(0, 0*x[x < 1], rev(y[x<1]), 0), col="mistyrose", border=NA))
lines(den1 <-density(absy1), col="blue")
with(den1, polygon(x=c(0, x[x < 1], rev(x[x<1]), 0), y=c(0, 0*x[x < 1], rev(y[x<1]), 0), col="aliceblue", border=NA))
abline(v=1,lty=2,col="black")
text(3.5,0.18,expression(abs(N(mu,3 * sigma^2)-mu)), col="blue")
text(1.75,0.48,expression(abs(N(mu,sigma^2)-mu)), col="red")
axis(1,at=0,labels=0, line=-1)
axis(1,at=1,labels=expression(sigma), line=-1)
The density function produces a side-effect of plotting a graph but it also return a list with named ‘x’ and ‘y’ components, so you need to save that as a named object (which I am naming ‘den1’) and work with those components. Since you haves some “slop” in your density diagram you might want to look at the ‘logspline’ package which ISTR has a density function that accepts constraints.
So do this instead as your initial plotting strategy::
And then add the colored polygon (‘sigma’ being 1 in this case):
Broken down into 4 components you are outlining the region with a starting point [0,0] then drawing along the x-axis to [sigma,0] then a vertical line to [sigma, density(sigma)], then going backwards along the density curve and closing back at [0,0].