简体   繁体   中英

R plot: drawing only part of a linear regression line

I can draw a partial linear regression line in a plot. For example a regression line for the last four values

sale5 <- c(6, 4, 9, 7, 6, 12, 8, 10, 9, 13)
plot(sale5)
abline(lsfit(tail(1:10,4), tail(sale5,4)))

But the regression line continues all the way back.

How can I draw only part of the regression line so that the regression line does not extending further back than four points in this case?

Thank you for your help.

I like to use ?clip for this sort of task:

plot(sale5)
clip(7,10,min(sale5),max(sale5))
abline(lsfit(tail(1:10,4), tail(sale5,4)))
# reset clipping to the extent of the plot region
do.call("clip", as.list(par("usr")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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