Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use geom_smooth with transformed y

Tags:

r

ggplot2

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)))))
like image 981
Jeroen Ooms Avatar asked Oct 01 '14 09:10

Jeroen Ooms


People also ask

What does Geom_smooth () using formula YX mean?

The warning geom_smooth() using formula 'y ~ x' is not an error. Since you did not supply a formula for the fit, geom_smooth assumed y ~ x, which is just a linear relationship between x and y.

What is the difference between Stat_smooth and Geom_smooth?

geom_smooth() and stat_smooth() are effectively aliases: they both use the same arguments. Use stat_smooth() if you want to display the results with a non-standard geom.

What is the use of Geom_smooth?

You can use the geom_smooth layer to look for patterns in your data. We use this layer to Plot two continuous position variables in the graph. The basic setting for described geometry is shown in the following plot.

What is the confidence interval in Geom_smooth?

Example 2: Modify Level of Confidence Interval By default, geom_smooth() uses 95% confidence bands but you can use the level argument to specify a different confidence level.


1 Answers

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

like image 134
mnel Avatar answered Sep 21 '22 08:09

mnel