This is just kind of annoying me, so I'm hoping someone has an idea. I'm running a multiple logistic regression where there is one numeric predictor and one categorical predictor. I'd like to make a nice looking ggplot of the logistic regression of the model (without an interaction term, so the curves should just be translations of each other). For example:
data("mtcars")
library(ggplot2)
glm(data = mtcars, vs ~ mpg + as.factor(gear))
Creates a model. One idea would be
ggplot(data = mtcars, aes(x = mpg, y = vs, color = as.factor(gear))) +
geom_point() +
geom_smooth(
method = "glm",
method.args = list(family = "binomial"),
se = F
)
but this creates a separate logistic model for each group, which is a different model. The best I've come up with is to use predict() with a response type and then add a geom_line() with y = prediction_value. This looks right-ish, but it's not as smooth as using geom_smooth. I know I could also use predict() on more points to smooth it out, but this seems like there must be a nicer way to do this.