Possible bug in geom_smooth() in ggplot 0.9.3.1

963 views
Skip to first unread message

Roey Angel

unread,
Apr 1, 2013, 6:23:24 PM4/1/13
to ggp...@googlegroups.com
Using ggplot 0.9.3.1 I'm trying to apply geom_smooth() on a plot but also choose the method myself.

Running:
df <- data.frame(x=seq(1:100), y=seq(100:1))

ggplot(df, aes(x, y)) +
    geom_point() +
    geom_smooth(method="gam")


Gives:
Error in get(as.character(FUN), mode = "function", envir = envir) :
  object 'gam' of mode 'function' was not found


But if I run once:
ggplot(df, aes(x, y)) +
    geom_point() +
    geom_smooth()


Then again:
ggplot(df, aes(x, y)) +
    geom_point() +
    geom_smooth(method="gam")


It works without complaining.

Why is that?

Thanks
Roey


Ista Zahn

unread,
Apr 1, 2013, 7:26:09 PM4/1/13
to Roey Angel, ggplot2
Where does the gam function come from? If you want to use the
function, you need to load the library. Having ggplot call the
smoothing function doesn't change that in this case. My guess is that
you want

library(mgcv)

Best,
Ista
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Roey Angel

unread,
Apr 2, 2013, 7:17:41 AM4/2/13
to ggp...@googlegroups.com, Roey Angel
Well generally speaking you're right. These methods do come from the mgcv library, but they should be either implemented in ggplot or loaded with it.
From the web page of stat_smooth():
"method
smoothing method (function) to use, eg. lm, glm, gam, loess, rlm. For datasets with n < 1000 default is loess. For datasets with 1000 or more observations defaults to gam, see gam for more details."

In any case, loading it does allow to run method="gam" or method="loess":

ggplot(df, aes(x, y)) +
    geom_point() +
    geom_smooth(method="gam")

But the results are totally different from running the code without specifying the method. Which is weird.

Does anyone know what's up with this?

Ista Zahn

unread,
Apr 2, 2013, 8:23:34 AM4/2/13
to Roey Angel, ggplot2
On Tue, Apr 2, 2013 at 7:17 AM, Roey Angel <an...@mpi-marburg.mpg.de> wrote:
Well generally speaking you're right. These methods do come from the mgcv library, but they should be either implemented in ggplot or loaded with it.

​I think that's debatable. I don't like it when packages load a bunch of other packages, because eventually you end up with masked function names and can lose track of which function is coming from where.​

 
From the web page of stat_smooth():
"method
smoothing method (function) to use, eg. lm, glm, gam, loess, rlm. For datasets with n < 1000 default is loess. For datasets with 1000 or more observations defaults to gam, see gam for more details."

In any case, loading it does allow to run method="gam" or method="loess":

ggplot(df, aes(x, y)) +
    geom_point() +
    geom_smooth(method="gam")

But the results are totally different from running the code without specifying the method. Which is weird.

Does anyone know what's up with this?


​When I run this example:

dat.large <- data.frame(x=rnorm(10000), y=rnorm(10000))
ggplot(dat.large, aes(x=x, y=y)) + geom_smooth()

ggplot tells me "geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method." And indeed running

ggplot(dat.large, aes(x=x, y=y)) + geom_smooth(method="gam", formula = y ~ s(x, bs = "cs"))

​gives the same results.

Roey Angel

unread,
Apr 2, 2013, 10:49:09 AM4/2/13
to ggp...@googlegroups.com, Roey Angel
Thanks for the help!
At least now with running: geom_smooth(method="gam", formula = y ~ s(x, bs = "cs"))
instead of:
geom_smooth(method="gam") gives the right result (i.e. same as when running geom_smooth()).

Still, even with your example if you reverse the order of the lines you should see what I mean.

run in a new R session:
dat.large <- data.frame(x=rnorm(10000), y=rnorm(10000))
ggplot(dat.large, aes(x=x, y=y)) + geom_smooth(method="gam", formula = y ~ s(x, bs = "cs"))
ggplot(dat.large, aes(x=x, y=y)) + geom_smooth()



And you should get the same error.

Thanks again,
Roey

Winston Chang

unread,
Apr 2, 2013, 12:09:49 PM4/2/13
to Roey Angel, ggp...@googlegroups.com
If you're interested, here's the code that attempts to load mgcv and use gam -- you can see that it only tries to do this when the size of at least one group is >1000. Notice that it only auto-loads mgcv in cases where it automatically chooses gam; when you specify model='gam' manually, it won't do it:

-Winston
Reply all
Reply to author
Forward
0 new messages