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.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
coding_heart
  • 1,245
  • 3
  • 25
  • 46
  • 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`? – davechilders Oct 09 '15 at 18:49
  • Yes, that's exactly right. I'll add that text to the question as well. – coding_heart Oct 09 '15 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](http://stackoverflow.com/questions/2329851/how-can-i-add-another-layer-new-series-to-a-ggplot) and [here](http://stackoverflow.com/questions/9109156/ggplot-combining-two-plots-from-different-data-frames) to start. – aosmith Oct 09 '15 at 20:34

1 Answers1

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

RHA
  • 3,677
  • 4
  • 25
  • 48