防止 R 在图表区域之外绘制线条 [英] Keep R from graphing a line outside of a chart's area

查看:44
本文介绍了防止 R 在图表区域之外绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止将最后一个分布的红线绘制在下图中的绘图区域之外?

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?

图表 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.

推荐答案

是的,正如 Joran 提到的,它正在绘制绘图区域外的线,因为我之前运行了 par(xpd=TRUE)在会话中尝试将图例放在外面.我只是运行 par(xpd=FALSE) 并解决了问题.

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.

这篇关于防止 R 在图表区域之外绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆