简体   繁体   English

当我没有任何数据点时(在R中)如何绘制abline()

[英]How do I plot an abline() when I don't have any data points (in R)

I have to plot a few different simple linear models on a chart, the main point being to comment on them. 我必须在图表上绘制一些不同的简单线性模型,主要要点是对它们进行评论。 I have no data for the models. 我没有模型数据。 I can't get R to create a plot with appropriate axes, ie I can't get the range of the axes correct. 我无法获得R来创建具有适当轴的绘图,即无法获得正确的轴范围。 I think I'd like my y-axis to 0-400 and x to be 0-50. 我想将y轴设置为0-400,将x设置为0-50。 Models are: $$ \\widehat y=108+0.20x_1 $$$$ \\widehat y=101+2.15x_1 $$$$ \\widehat y=132+0.20x_1 $$$$ \\widehat y=119+8.15x_1 $$ I know I could possibly do this much more easily in a different software or create a dataset from the model and estimate and plot the model from that but I'd love to know if there is a better way in R. 型号为:$$ \\ widehat y = 108 + 0.20x_1 $$$$ \\ widehat y = 101 + 2.15x_1 $$$$ \\ widehat y = 132 + 0.20x_1 $$$$ \\ widehat y = 119 + 8.15x_1 $ $我知道我可以在其他软件中更轻松地完成此操作,或者从模型创建数据集,然后据此估算和绘制模型,但是我很想知道R中是否有更好的方法。

As @Glen_b noticed, type = "n" in plot produces a plot with nothing on it. 正如@Glen_b注意到的那样,在plot中type = "n"会生成一个没有任何内容的图。 As it demands data, you have to provide anything as x - it can be NA , or some data. 由于它需要数据,因此您必须提供x 任何东西 -它可以是NA或某些数据。 If you provide actual data, the plot function will figure out the plot margins from the data, otherwise you have to choose the margins by hand using xlim and ylim arguments. 如果提供实际数据,则plot函数将从数据中找出绘图边距,否则,您必须使用xlimylim参数手动选择边距。 Next, you use abline that has parameters a and b for intercept and slope (or h and v if you want just a horizontal or vertical line). 接下来,使用具有参数ab abline截距和斜率(如果只需要水平或垂直线,则使用hv )。

plot(x=NA, type="n", ylim=c(100, 250), xlim=c(0, 50),
     xlab=expression(x[1]), ylab=expression(hat(y)))
abline(a=108, b=0.2, col="red")
abline(a=101, b=2.15, col="green")
abline(a=132, b=0.2, col="blue")
abline(a=119, b=8.15, col="orange")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM