ggplot错误:找到的对象不是stat

时间:2017-05-01 17:27:36

标签: r ggplot2 geom-bar

ggplot() +
  geom_point(aes(x = Africa_set$Africa_Predict, y = Africa_set$Africa_Real), color ="red") +
  geom_line(aes(x = Africa_set$Africa_Predict, y = predict(simplelm, newdata = Africa_set)),color="blue") +
  labs(title = "Africa Population",fill="") +
  xlab("Africa_set$Africa_Predict") + 
  ylab("Africa_set$Africa_Real")

然后显示错误消息:

Error: Found object is not a stat

如何修复此错误?

1 个答案:

答案 0 :(得分:0)

看起来你正试图用顶部的拟合回归线绘制点。你可以这样做:

library(ggplot2)

ggplot(iris, aes(Petal.Length, Petal.Width)) +
  geom_point() +
  geom_smooth(method = "lm")

或者,如果你确实想要使用你在simplelm对象中预先存储的模型,就像你的示例中那样,你可以使用扫帚包中的augment

library(ggplot2)
library(broom)

simplelm <- lm(Petal.Width ~ Petal.Length, data = iris)


ggplot(data = augment(simplelm),
         aes(Petal.Length, Petal.Width)) +
  geom_point() +
  geom_line(aes(Petal.Length, .fitted), color = "blue")