I want to add a fitted line of a quadratic fit to a scatteprlot, but the ordering of the points is somehow messed up.
attach(mtcars)
plot(hp, mpg)
fit <- lm(mpg ~ hp + I(hp^2))
summary(fit)
res <- data.frame(cbind(mpg, fitted(fit), hp))
with(res, plot(hp, mpg))
with(res, lines(hp, V2))
This draws lines all over the place, as opposed to the smooh fit through the scatterplot. I'm sure this is pretty straightforward, but I'm a little stumped.