I am hoping to replicate a gam
in geom_smooth
but in plotly
(not ggplotly
).
I can't get the model to match up though. The gam
using mgcv
doesn't match up though. They seem to be standardised or something. What am I doing wrong? Is there an argument missing anywhere? I had a look at the documentation, but couldn't work it out. Can you help?
library(gapminder)
library(tidyverse)
library(plotly)
df2 <- gapminder %>%
filter(country == "Afghanistan")
ggplot(df2, aes(year, gdpPercap)) +
geom_point() +
geom_smooth(method = "gam", method.args = list(family = "quasipoisson"))
But the mgcv
version seems to be standardised or something.
# test gam
gam2 <- mgcv::gam(gdpPercap ~ s(year), data = df2, family = "quasipoisson")
summary(gam2)
plot(gam2)
And then obviously the plotly
version is wrong.
# test plotly
plot_ly(df2, x = ~year, y = ~gdpPercap, type = 'scatter', name = 'points') %>%
add_trace(y = predict(gam2), type = 'scatter', mode = 'lines')
I am hoping to use plotly
rather than ggplotly
(where it is fine) as I want to add a couple of other things later that can only be done in plotly.