2019-08-10

How to plot a log curve in R

Problem

We want to fit a logarithmic curve to a scatterplot.

library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
  geom_point()

Solution

Base package

plot(hwy ~ displ, mpg)
logEstimate <- lm(hwy ~ log(displ), data = mpg)
curve(coef(logEstimate)[1] + coef(logEstimate)[2] * log(x), add = TRUE)
ggplot2

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  stat_smooth(method = "lm", formula = y ~ log(x))

References

No hay comentarios:

Publicar un comentario

Nube de datos