ggplot error: Found object is not a stat
ggplot error: Found object is not a stat
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
如何解决这个错误?
您似乎正在尝试在顶部绘制带有拟合回归线的点。您可以使用以下方法执行此操作:
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_smooth(method = "lm")
或者,如果您确实想像示例中那样使用提前存储在 simplelm
对象中的模型,则可以使用 broom 包中的 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")
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
如何解决这个错误?
您似乎正在尝试在顶部绘制带有拟合回归线的点。您可以使用以下方法执行此操作:
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_smooth(method = "lm")
或者,如果您确实想像示例中那样使用提前存储在 simplelm
对象中的模型,则可以使用 broom 包中的 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")