2

I performed regression analysis. Now i need create plot depended vs prediction, where depend is initial values of dependent var(mpg) and predict is predicted values.

 house_test=lm(mpg ~ 1 + hp + wt, data=mtcars)

prediction <- as.data.frame(predict(house_test, mtcars, 
                                    interval = 'prediction',
                                    level = .95))


prediction$mpg <- mtcars$mpg


ggplot(prediction) +
  geom_ribbon(aes(mpg, ymin = lwr, ymax = upr), fill = 'lightskyblue', alpha = 0.5) +

  geom_point(aes(mpg, fit), alpha = 0.2) +
  labs(title = "interval 95%CI", y = "depend", x = "predict")

How can i add the diagonal line(yellow color) in depended vs predict plot? Note Y axis is dependent var and X asis is prediction values.

i expect in output plot dep-pred

psysky
  • 3,037
  • 5
  • 28
  • 64
  • Check out `geom_abline()`. – moooh May 27 '18 at 16:02
  • @moooh, yes, i have added geom_abline() and got the plot without points. just check ggplot(prediction) + geom_ribbon(aes(mpg, ymin = lwr, ymax = upr), fill = 'lightskyblue', alpha = 0.5) + geom_abline() geom_point(aes(mpg, fit), alpha = 0.2) + labs(title = "interval 95%CI", y = "depend", x = "predict") . Maybe i did wrong? – psysky May 27 '18 at 16:08
  • @varimax, the code in your comment works for me; expect that there is a "+" missing between "geom_abline" and "geom_point" – otwtm May 27 '18 at 16:15

1 Answers1

1
ggplot(prediction) +
  geom_ribbon(aes(mpg, ymin = lwr, ymax = upr), fill = 'lightskyblue', alpha = 0.5) +

  geom_point(aes(mpg, fit), alpha = 0.2) +
  labs(title = "interval 95%CI", y = "depend", x = "predict") +

  geom_abline(intercept = 0, slope = 1, col="yellow")
otwtm
  • 1,779
  • 1
  • 16
  • 27