user3170629
user3170629

Reputation: 499

controlling length of the line of best fit in R

I have plotted a graph which has a line of best fit. However, I want to make sure the line isn't touching the y- or x-axis. Is there a way to control its length?

I am currently using the code:

plot(Perimeter~Average.tortuosity, data=T.R,xlim=c(0,5), xlab="Path tortuosity", ylab="Territory perimeter", pch=19)
abline(lm(Perimeter~Average.tortuosity, data=T.R))

Thanks

Upvotes: 0

Views: 1465

Answers (1)

MrFlick
MrFlick

Reputation: 206526

If you only want to plot part of the regression line, you'll have to extract the points yourself. Abline draws a "line" which extends infinitely in both directions. But you can use the predict function to get a y for any x.

#sample data
dd<-data.frame(x=1:10, y=cumsum(runif(10)))

plot(y~x, dd)
mm<-lm(y~x, dd)

#lines from 4 to 8
nx<-c(4,8)
ny<-predict(mm, newdata=data.frame(x=nx))
lines(nx,ny, col="red")

And that results in

enter image description here

Upvotes: 3

Related Questions