I fitted a linear regression model to my simulated data:
Call:
lm(formula = y ~ x, data = mydata)
Coefficients:
(Intercept) x
1.5770 0.8014
I am wondering if there is a way to display the data points above and below the regression line in different colors.
mydata <- structure(list(x = 1:10, y = c(3.28883727809953, 2.46416802959371,
5.21809941024356, 4.05952314018145, 5.32088305796252, 5.37511676113549,
7.13585900360674, 7.93915982555469, 7.61133858936195, 11.4349438464509
)), class = "data.frame", row.names = c(NA, -10L))
Here is a demo:
## fit regression line
fit <- lm(y ~ x, data = mydata)
## above the line (observed > fitted is TRUE): TRUE + 1 is 2, select "green"
## below the line (observed > fitted is FALSE): FALSE + 1 is 1, select "red"
ptcol <- c("red", "green")[(mydata$y > fit$fitted.values) + 1]
## plot observed data points
with(mydata, plot(x, y, col = ptcol))
## add regression line
abline(fit)