How can I use stat_smooth to display one line on a two-factor figure?

I have 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 performed a logistic regression:

lm <- glm(data = df, factor(d) ~ a + b + c, family = binomial)

The predicted probabilities are produced:

df$pred <- predict(lm, type = "response")

And I would like to draw a result with both a jitter graph for a, and a bfill color for dand a smooth line (using geom_smooth) for just an effect aond

I 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 this does not give what I would like. I would like this line:

ggplot(data = df , aes(x=a, y = pred)) + geom_smooth()

superimposed on this:

ggplot(data = df , aes(x=a, y = b, color = factor(d))) + geom_jitter()

Any help would be appreciated.

+4
source share
2 answers

This does the trick, now it no longer groups it by a factor:

  ggplot(data = df ) +  
  geom_point(aes(x=a, y = b, color = factor(d))) +
  geom_smooth(aes(x=a, y = pred))

ggplot, . , (a b), - .

, y . geom_smooth() s- . Y- 0,51 0,47.

enter image description here

0 1. , . enter image description here

+2

(MichaelVE) . , , : http://www.ats.ucla.edu/stat/r/faq/smooths.htm

:

aes(group = 1) stat_smooth ( )

ggplot(mtcars, aes(x = hp, y = mpg, colour = factor(vs))) + geom_point() +
  stat_smooth(aes(group = 1), method = "lm", formula = y ~ x, se = FALSE)

- geom_point, ggplot ( ):

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(colour = factor(vs))) +
  stat_smooth(method = "lm", formula = y ~ x, se = FALSE)
+1

All Articles