I have a data.frame as such:
df <- data.frame(a = runif(1000), b = runif(1000), c = runif(1000), d = sample(c("yes", "no"), 1000, replace=TRUE))
And I ran a logistic regression:
lm <- glm(data = df, factor(d) ~ a + b + c, family = binomial)
Produced the predicted probabilities:
df$pred <- predict(lm, type = "response")
And would like to graph the result with both a jitter plot for a
and b
, a color fill for d
and a smooth line (using geom_smooth) for simply the effect of a
on d
I've tried this:
ggplot(data = df , aes(x=a, y = b, color = factor(d))) + geom_jitter() +
geom_smooth(data = df, aes(x=a, y = pred))
But it's not producing what I would like. What I would like is this line:
ggplot(data = df , aes(x=a, y = pred)) + geom_smooth()
overlaid on this:
ggplot(data = df , aes(x=a, y = b, color = factor(d))) + geom_jitter()
Any and all help would be appreciated.
This does the trick, now it doesn't group it per factor anymore:
ggplot(data = df ) +
geom_point(aes(x=a, y = b, color = factor(d))) +
geom_smooth(aes(x=a, y = pred))
You now make a ggplot where you specify the data. On top of that you add a layer with points (a and b) and on top of that the geom smooth line.
But make sure that you also have a look at the y-axis of both plots. In your geom_smooth() plot you see the nice s-shape curve. However, the Y-axis range is from 0.51 to 0.47.
If we then look at your total plot with limits of 0 and 1. Your line looks almost straight which is just because of the limits.
The other solution (by MichaelVE) does not work for me. However, I've found how to solve it here: http://www.ats.ucla.edu/stat/r/faq/smooths.htm
In short:
setting aes(group = 1)
in stat_smooth
(but legend has additional lines)
ggplot(mtcars, aes(x = hp, y = mpg, colour = factor(vs))) + geom_point() +
stat_smooth(aes(group = 1), method = "lm", formula = y ~ x, se = FALSE)
or better - using factor fields only in geom_point
, not ggplot
(this thing has clean legend):
ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(colour = factor(vs))) +
stat_smooth(method = "lm", formula = y ~ x, se = FALSE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With