Here is some data and a model. It consists of a linear and quadratic predictor (a and a2) and a linear control variable (b).
library(data.table)
library(ggplot2)
d <- as.data.table(cbind(a = rnorm(50), b = rnorm(50), y = rnorm(50)))
d$a2 <- (d$a)^2
m <- lm(y ~ a + a2 + b, data = d)
I would like to plot the linear and quadratic effects, while also controlling for b.
I have found how to do this if I want only the effects of a and a2:
ggplot(d,
aes(x = a, y = y)) +
geom_point() +
geom_smooth(method = "lm",
formula = y ~ x,
aes(color = "linear"),
se = FALSE) +
geom_smooth(method = "lm",
formula = y ~ x + I(x^2),
aes(color = "quadratic"),
se = FALSE) +
theme_bw()
But how can I plot this while also controlling for b?