In ggplot
, you can include regression lines using
geom_abline()
. For the next example, we set up the same graph of patient height against weight that we have used several times before:
P <- ggplot(T, aes(x = HEIGHT, y = WEIGHT_1)) + geom_point()
As a start, let's calculate the slope and intercept of the line of best fit (regression line) for height against weight before treatment. In Chapter 1, Base Graphics in R – One Step at a Time, in the section entitled Including a regression line, we saw how to include a linear regression line on a graph. Now, we use the lm()
command again to fit a linear regression model by using the following syntax:
lm(WEIGHT_1 ~ HEIGHT, data = T)
Here is the output that you will see on your screen:
Call: lm(formula = WEIGHT_1 ~ HEIGHT, data = T) Coefficients: (Intercept) HEIGHT -123.611 1.166
So, the intercept is approximately -123.61 and the slope is approximately 1.17. You can now include the regression line...