bizzle
bizzle

Reputation: 11

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, i.e. I can't get the range of the axes correct. I think I'd like my y-axis to 0-400 and x to be 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.

Upvotes: 1

Views: 1482

Answers (1)

Tim
Tim

Reputation: 7464

As @Glen_b noticed, type = "n" in plot produces a plot with nothing on it. As it demands data, you have to provide anything as x - it can be NA, or some data. 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. 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).

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")

Upvotes: 2

Related Questions