How can I use stat_smooth to show one line, on a two factor figure?

£可爱£侵袭症+ 提交于 2021-02-05 05:01:02

问题


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.


回答1:


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.

enter image description here

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. enter image description here




回答2:


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)


来源:https://stackoverflow.com/questions/30563896/how-can-i-use-stat-smooth-to-show-one-line-on-a-two-factor-figure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!