title: color a portion of the normal distribution
body: <p>I would like to fill or color a portion of the normal distribution.  I fould an example of how to do that here:</p>
  
  <p><a href="http://msenux.redwoods.edu/math/R/StandardNormal.php" rel="nofollow">http://msenux.redwoods.edu/math/R/StandardNormal.php</a></p>
  
  <pre><code>x=seq(-4,4,length=200)
  y=dnorm(x)
  plot(x,y,type="l", lwd=2, col="blue")
  x=seq(-1,1,length=100)
  y=dnorm(x)
  polygon(c(-1,x,1),c(0,y,0),col="gray")
  </code></pre>
  
  <p>However, if I try to generalize the above code for any portion of the x-axis the best I can come up with is:</p>
  
  <pre><code>sigma &lt;- 1
  mu    &lt;- 0
  
  lower.x &lt;- -0.0
  upper.x &lt;-  2.1
  
  x  &lt;- seq(-4, 4, length = 200)
  y  &lt;- ( 1/(sigma * sqrt(2*pi)) ) * ( exp(1)^( (-1 * ((x - mu)^2)) / (2*(sigma^2)) ) )
  plot(x,y,type="l", lwd=2, col="blue")
  
  x=seq(lower.x, upper.x, length=100)
  y  &lt;- ( 1/(sigma * sqrt(2*pi)) ) * ( exp(1)^( (-1 * ((x - mu)^2)) / (2*(sigma^2)) ) )
  polygon(c(lower.x,x,1), c(0,y,0), col="gray")
  </code></pre>
  
  <p>I think the error involves the second length statement, but I cannot figure out how to correct the mistake.  Also, I would prefer that the blue line of the normal distribution curve not be covered by the border of the shaded region.  Although that is of less concern.  Thank you for any advice.</p>