How can I display multiple regression lines in ggplot2 without an interaction?

Viewed 113

I'm doing an analysis of covariance, modelling how a continuous variable (let's call it contvar) affects response, for each of three levels of a categorical factor (catfact).

At first I made graphs with ggplot2 that showed the three linear regressions, either faceted so with three panels, or all on the same panel. I used this code to add the regression lines:

+ geom_smooth(aes(x = contvar, y = response, colour = catfact), show.legend = FALSE, formula = 'y ~ x', method = "lm", se = FALSE)

Having analysed the full model allowing for an interaction between contvar and catfact, I found the interaction term to be non-significant, so I removed it to generate a minimal adequate model as so:

minimal_adequate_model <- lm(response ~ contvar + catfact, data = data)

How should I now go about plotting the three regression lines again, but this time so that they all have the same gradient (the gradient extracted from minimal_adequate_model)?

I did find a work-around (below) but this seems really clunky, and isn't ideal for controlling aesthetics etc.

#First I made the equations for each level of catfact as objects separately:
lvl1_equation <- function(x){coef(minimal_adequate_model)[2] * x + coef(minimal_adequate_model)[1]}
lvl2_equation <- function(x){coef(minimal_adequate_model)[2] * x + coef(minimal_adequate_model)[1] + coef(minimal_adequate_model)[3]}
lvl3_equation <- function(x){coef(minimal_adequate_model)[2] * x + coef(minimal_adequate_model)[1] + coef(minimal_adequate_model)[3] + coef(minimal_adequate_model)[4]}

#And then I included them in the ggplot2 like this:
+ stat_function(fun = control_equation,geom="line", col = "#f0746e")
+ stat_function(fun = low_equation,geom="line", col = "#dc3977")
+ stat_function(fun = max_equation,geom="line", col = "#7c1d6f")

I feel like I may be missing something obvious here, so I'd be really grateful for any thoughts.

0 Answers
Related