【问题标题】:how to define graphical bounds of abline linear regression in R如何在 R 中定义 abline 线性回归的图形边界
【发布时间】:2014-12-15 20:14:39
【问题描述】:

我正在尝试截断 abline 的末端,这实际上只是我的数据的线性回归。

fit1=lm(logy~logx)

> fit1

Call:
lm(formula = logy ~ logx)

Coefficients:
(Intercept)         logx  
     -5.339       -2.115 

其中 logx 是 log10(x[1:365] 转换的。 logy 遵循相同的代码。当我使用abline(fit1,col="red") 绘图时,我得到了我想要的线,但线超出了我最初设置的边界[1:365]。我已经尝试过par=xpd,但这并没有将线路减少到我想要的限制。我玩过segments() 无济于事。也许是line() 参数?

编辑 这是新的解决方案:

#the following vectors x and y store our data that we want to plot
x<-(1:10)
y<-(10:1)
plot(x,y,type="l",log="xy")
#we want to do a linear regression on our log10 transformed data and add the line to the plot
logy=log10(y[3:8])
logx=log10(x[3:8])
fit1=lm(logy~logx)
#finally, we want the regression line to only seem like it applies over the range from 3 to 8
clip(3,8,8,3)
abline(fit1,col="red")

这会产生一个图,其中有一条线(现在,没有)在我们的 x 轴上延伸超过 3 到 8。我只希望回归线显示从 x=3 到 x=8,遵循相同的斜率。

【问题讨论】:

  • 请提供一个可重现的例子。
  • 您可能必须使用 segment() 并在 x = 3 和 x = 8 处进行评估
  • 我尝试使用segment(),但我认为我不理解 R 帮助数据库中的语法。需要详细说明吗?

标签: r plot linear-regression


【解决方案1】:

你可以clip绘图区。我不能使用您的数据,因为您的示例不可重现,但这里有一个说明:

> plot(1,1,type='n',xlim=c(0,400), ylim=c(0,10))
> clip(1,365,0,10)
> abline(h=5,col='red')

在坐标框x0=1,x1=365,y0=0,y1=10 内生成一条线:

【讨论】:

  • 感谢您的帮助。对我来说,关键是确保我启动了一个设备 png(filename="test") 并将 clip() 作为它自己的参数放在我不想剪辑的所有内容之后。
【解决方案2】:

您可以使用segments()

例如:

segments(x0=3, # Value from x (initial)
         x1=8, # Value to x (final)
         y0=5, # Value from y (initial)
         y1=5, # Value to y (final)
         col='red')

【讨论】:

    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2013-11-04
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多