简体   繁体   中英

Plot 2 different models on same ggplot2

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.

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: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM