1

I am running the same set of multivariate models for different dependent variables. I would like to generate predicted values of the different y's versus x, based on a model controlling for z and then plot them in the same ggplot2 such that each model gets a different color. The following code generates two models for the relationship between x and y1 and y2 controlling for z, the uncertainty and then only plots one of them:

require(ggplot2)
set.seed(123)
dat <- data.frame(x = rnorm(100), z = rnorm(100), y1 = rnorm(100), y2 = rnorm(100))
dat1 <- dat[,c(1,2,3)]
dat2 <- dat[,c(1,2,4)]

mod1 <- lm(y1 ~ x + z, data = dat1)
mod2 <- lm(y2 ~ x + z, data = dat2)

dat1$mod1 <- predict(mod1, newdata =dat1)  
err <- predict(mod1, newdata =dat1, se = TRUE)   
dat1$ucl <- err$fit + 1.96 * err$se.fit
dat1$lcl <- err$fit - 1.96 * err$se.fit   

dat2$mod2 <- predict(mod2, newdata =dat2)  
err <- predict(mod2, newdata =dat2, se = TRUE)   
dat2$ucl <- err$fit + 1.96 * err$se.fit
dat2$lcl <- err$fit - 1.96 * err$se.fit   

ggplot(dat1) + 
        geom_point(aes(x = x, y = mod1), size = .8, colour = "black") +
        geom_smooth(data = dat1, aes(x= x, y = mod1, ymin = lcl, ymax = ucl), size = 1, colour = "darkblue", se = TRUE, stat = "smooth", method = "lm")

Any idea on how to add the second plot on this given that the frames are different?

Thanks for any and all help.

3
  • Can you describe your goal in words. Do you wish to plot the predicted values of y versus x, based on a model controlling for z? With separate colors for y1 and y2? Commented Oct 9, 2015 at 18:49
  • Yes, that's exactly right. I'll add that text to the question as well. Commented Oct 9, 2015 at 18:50
  • Lots of options on how to do this, but based on the way you've currently approached the problem you likely just need to add additional layers based on different datasets. See here and here to start. Commented Oct 9, 2015 at 20:34

1 Answer 1

2

Just add new layers with a data=dat2 and y=mod2. I've made the dots and line red.

ggplot(dat1) + 
  geom_point(aes(x = x, y = mod1), size = .8, colour = "black") +
  geom_smooth(data = dat1, aes(x= x, y = mod1, ymin = lcl, ymax = ucl), size = 1, colour = "darkblue", se = TRUE, stat = "smooth", method = "lm") +
  geom_point(data=dat2, aes(x = x, y = mod2), size = .8, colour = "red") +
  geom_smooth(data = dat2, aes(x= x, y = mod2, ymin = lcl, ymax = ucl), size = 1, 
              colour = "red", se = TRUE, stat = "smooth", method = "lm")

Result: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

but this way doesn't include a legend

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.