0
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")

Then show the error message:

Error: Found object is not a stat

How can fix this error?

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Legend
  • 7
  • 3
  • When asking for help, be sure to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can run the code. But you really shouldn't be using `$` within `aes()` calls, you should be passing in a `data=` parameter and using proper column names. And maybe look at `geom_smooth` if you want to plot a regression line. – MrFlick May 01 '17 at 18:12

1 Answers1

0

It looks like you are trying to plot points with a fitted regression line on top. You can do this using:

library(ggplot2)

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

Or, if you really do want to use the model you've stored ahead of time in a simplelm object like you have in your example, you could use augment from the broom package:

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")
Julia Silge
  • 10,848
  • 2
  • 40
  • 48