6

Is there a way to use geom_smooth when the y variable in the formula is transformed? For example:

#This works:
 myplot <- qplot(speed, dist, data=cars)
(myplot + geom_smooth(method="lm", formula=y~log(x)))

#does not work
(myplot + geom_smooth(method="lm", formula=log(y)~x))

What I am after is a line like this:

myplot + geom_line(aes(x=speed, y=exp(predict(lm(log(dist)~speed)))))

1 Answer 1

7

You can fit a GLM for Gaussian (normally distributed) data and a log link. This will allow stat_smooth to use and return the appropriate predictions

(myplot + geom_smooth(method = "glm", formula = y~x,
                      family = gaussian(link = 'log')))

enter image description here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.