r - How can I use stat_smooth to show one line, on a two factor figure? -
i have data.frame such:
df <- data.frame(a = runif(1000), b = runif(1000), c = runif(1000), d = sample(c("yes", "no"), 1000, replace=true))
and ran logistic regression:
lm <- glm(data = df, factor(d) ~ + b + c, family = binomial)
produced predicted probabilities:
df$pred <- predict(lm, type = "response")
and graph result both jitter plot a
, b
, color fill d
, smooth line (using geom_smooth) 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 like. 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 , appreciated.
this trick, doesn't group per factor anymore:
ggplot(data = df ) + geom_point(aes(x=a, y = b, color = factor(d))) + geom_smooth(aes(x=a, y = pred))
you make ggplot specify data. on top of add layer points (a , b) , on top of geom smooth line.
but make sure have @ y-axis of both plots. in geom_smooth() plot see nice s-shape curve. however, y-axis range 0.51 0.47.
if @ total plot limits of 0 , 1. line looks straight because of limits.
Comments
Post a Comment