5
When you use the function stat_smooth
of ggplot2
, he performs the following steps:
I’ll use the database mtcars
, but you can easily replace it with your own.
modelo <- loess(mpg ~ hp, data = mtcars) # ajusta o modelo loess
mtcars$pred <- predict(modelo) # calcula as predicoes
ggplot (data = mtcars, aes (x = hp, y = mpg)) + geom_point() +
geom_line(aes(y = pred), colour = "blue") # plota as predicoes usando linha
See that this graph is equal to the graph made using:
ggplot (data = mtcars, aes (x = hp, y = mpg)) + geom_point() +
stat_smooth(method = "loess")
Therefore, to get the maximum value of the curve just filter in your date.frame:
mtcars[mtcars$pred == max(mtcars$pred), ]
That’s right, folks. In both ways I got the result I expected. Thanks! :)
– Samara