Michael Discenza
Michael Discenza

Reputation: 3330

Keep R from graphing a line outside of a chart's area

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?

chart http://i.minus.com/jRiGxDBVw6kjZ.jpeg

I generated the graph with the following code:

x <- seq(0,4,.1)
alpha_0 <- 2
beta_0 <- .2
hist(rexp(256, rate=1))
sample <- 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))

Sorry, seems like a pretty simple fix, I just couldn't figure it out from the documentation. Thanks for your help.

Upvotes: 1

Views: 9009

Answers (1)

Michael Discenza
Michael Discenza

Reputation: 3330

Yes, as Joran mentioned, it was graphing the line outside of the plot area because I ran par(xpd=TRUE) earlier in the session to try to put the legend outside. I simply ran par(xpd=FALSE) and it solved the problem.

Upvotes: 3

Related Questions