Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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.

like image 611
gh0strider18 Avatar asked May 31 '15 23:05

gh0strider18


Video Answer


2 Answers

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

like image 100
MichaelVE Avatar answered Sep 30 '22 05:09

MichaelVE


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)
like image 25
Piotr Migdal Avatar answered Sep 30 '22 06:09

Piotr Migdal