user10102227
user10102227

Reputation:

Error in int_abline....plot.new has not been called yet, how to put line of best fit?

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

Answers (1)

mischva11
mischva11

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)

plot

solution with ggplot

q <- qplot(x = wt, y = mpg, data = mtcars, color = am)
q + geom_abline(intercept=17.147, slope=7.245)

ggplot

Upvotes: 1

Related Questions