You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
geom_smooth throws error with diamonds dataset #2621
ggplot(diamonds, aes(x=carat, y=price)) + geom_smooth()
#> `geom_smooth()` using method = 'gam' and formula 'y ~ mgcv::s(x, bs = "cs")'#> Warning message:#> Computation failed in `stat_smooth()`:#> invalid type (list) for variable 'mgcv::s(x, bs = "cs")'
At first I thought it was because diamonds were a nibble but converting to a data.frame preserves the error...
This works on the CRAN version so it is a regression introduced somewhere. Strangely enough, it is tied to something in the diamonds dataset as geom_smooth works fine with e.g. mtcars
The problem is not tied to diamonds, and it arises specifically when using splines with the GAM model. It must be a recent regression, because this definitely worked 1-2 months ago.
library(ggplot2)
ggplot(mtcars, aes(x=hp, y=mpg)) + geom_smooth(method="loess") # loess is default for small datasets
Also, one more comment: I added a regression test for geom_smooth() but it only tests the loess method. I think we need to add a regression test for the GAM method as well. This would have been a pretty disastrous bug to have in a released ggplot2.
mgcv::gam(mpg~mgcv::s(wt), data=mtcars)
#> Error in model.frame.default(formula = mpg ~ mgcv::s(wt), data = mtcars, : invalid type (list) for variable 'mgcv::s(wt)'
One solution might be to create a custom environment in which s <- mgcv::s and make sure the gam expression is evaluated in that environment. That exceeds my R abilities, though. The other solution would be to somehow fix this in the mgcv package.
Activity
clauswilke commentedon May 17, 2018
The problem is not tied to
diamonds, and it arises specifically when using splines with the GAM model. It must be a recent regression, because this definitely worked 1-2 months ago.Created on 2018-05-17 by the reprex package (v0.2.0).
clauswilke commentedon May 17, 2018
Also, one more comment: I added a regression test for
geom_smooth()but it only tests the loess method. I think we need to add a regression test for the GAM method as well. This would have been a pretty disastrous bug to have in a released ggplot2.hadley commentedon May 17, 2018
It seems to be the namespace that causes the problem: this is ok
hadley commentedon May 17, 2018
Minimal reprex
This was introduced in #2535, cc @bfgray3.
clauswilke commentedon May 17, 2018
Yes, correct. Reprex outside of ggplot2:
Created on 2018-05-17 by the reprex package (v0.2.0).
hadley commentedon May 17, 2018
So we just need to revert that change including the news bullet, and add a test.
I have a dim recollection that we've done this dance before.
bfgray3 commentedon May 17, 2018
Apologies for introducing this bug.
clauswilke commentedon May 17, 2018
I think this raises two questions, though: 1. Why did it work when @bfgray3 proposed the change originally? 2. Is there a reliable way to fix #2535?
clauswilke commentedon May 17, 2018
I did some more digging.
gam()looks specifically for a term calleds(as well aste,ti, and a few others), so it doesn't recognizemgcv::s:Created on 2018-05-17 by the reprex package (v0.2.0).
One solution might be to create a custom environment in which
s <- mgcv::sand make sure thegamexpression is evaluated in that environment. That exceeds my R abilities, though. The other solution would be to somehow fix this in the mgcv package.fix geom_smooth() and add regression tests. Closes tidyverse#2621.
hadley commentedon May 17, 2018
For now I think we should forward to gam maintainer.
clauswilke commentedon May 18, 2018
I have contacted the gam maintainer.
fix geom_smooth() and add regression tests. Closes #2621. (#2626)
lock commentedon Nov 14, 2018
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/