Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logarithmic y with geom_smooth

Tags:

r

ggplot2

This question is similar to this one: Use geom_smooth with transformed y

In fact, it's the same one, it's just that the solution provided there no longer works.

What I want to do is plot a geom_smooth that has log(y) on the y side of the formula. If done directly in the formula argument, it gives a strange result. So, I will use the same example used in the question that I mentioned:

#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))

#no longer works:
(myplot + geom_smooth(method = "glm", formula = y~x,
                  family = gaussian(link = 'log')))

What I am after is a line like this:

myplot + geom_line(aes(x=speed, y=exp(predict(lm(log(dist)~speed)))))
like image 767
Sollano Rabelo Braga Avatar asked Dec 16 '16 17:12

Sollano Rabelo Braga


1 Answers

Yes, you're right, it seems the necessary syntax has changed a bit:

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

enter image description here

like image 170
Hack-R Avatar answered Oct 15 '22 22:10

Hack-R