title: Keep R from graphing a line outside of a chart's area
body: <p>How do I prevent the red line for the last distribution from being plotted outside of the area of the plot in the graph below?</p>
  
  <p><a href="http://i.minus.com/jRiGxDBVw6kjZ.jpeg" rel="nofollow noreferrer">chart http://i.minus.com/jRiGxDBVw6kjZ.jpeg</a></p>
  
  <p>I generated the graph with the following code:</p>
  
  <pre><code>x &lt;- seq(0,4,.1)
  alpha_0 &lt;- 2
  beta_0 &lt;- .2
  hist(rexp(256, rate=1))
  sample &lt;- rexp(256, rate=1)
  plot(x,dgamma(x, shape=alpha_0, rate=beta_0),type='l',col='black',ylim=c(0,2),main="Posteriors of Exponential Distribution", ylab='')
  
  lines(x,dgamma(x, shape=alpha_0+4, rate=beta_0+sum(sample[1:4])),col='blue')
  lines(x,dgamma(x, shape=alpha_0+8, rate=beta_0+sum(sample[1:8])),col='green')
  lines(x,dgamma(x, shape=alpha_0+16, rate=beta_0+sum(sample[1:16])),col='orange')
  lines(x,dgamma(x, shape=alpha_0+256, rate=beta_0+sum(sample[1:256])),col='red',)
  legend(x=2.5,y=2, c("prior","n=4", "n=8", "n=16", 'n=256'), col = c('black', 'blue', 'green','orange' ,'red'),lty=c(1,1,1,1))
  </code></pre>
  
  <p>Sorry, seems like a pretty simple fix, I just couldn't figure it out from the documentation.  Thanks for your help.</p>