Reputation:
I'm trying to put a line of best fit on a graph but I'm getting this error
I've already changed the order of where I put "abline(17.147, 7.245)", I've also tried to add plot.new() but that doesn't work
print(lm(mtcars$mpg ~ mtcars$am))
#the coefficients from above are 17.147 and 7.245
q <- qplot(x = wt, y = mpg, data = mtcars, color = am)
q
abline(17.147, 7.245)
I expect a line of best fit on my graph
Upvotes: 0
Views: 1269
Reputation: 2956
you are mixing ggplot
and plot
functions. Those can't be mixed easily, so just use one plotting style.
solution with plot
:
plot(mtcars$wt, mtcars$mpg)
abline(17.147, 7.245)
solution with ggplot
q <- qplot(x = wt, y = mpg, data = mtcars, color = am)
q + geom_abline(intercept=17.147, slope=7.245)
Upvotes: 1