Friday, August 3, 2012

How to add line to a plot in R

Lets say you have a scatter plot and want to make a linear regression line.

For example we will create two variables and plot them in a scatter plot.
a <- c(1:100)
b <- rnorm(100, 5, 0.5)

For linear regression line type:
abline(lm(b~a), col=14, lwd=2)

If you want to add custom line you could also use abline command, with the parameters a=intersection with y axes and b=slope of the curve (hence the name of the command abline)
For example:
abline(5,0, col=14, lwd=2)

No comments:

Post a Comment