Skip to content

geom_smooth doesn't work when function is entered with :: #2870

Closed

Description

@IndrajeetPatil
# loading needed libraries
library(ggplot2)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-24. For overview type 'help("mgcv-package")'.

# without `::`
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_smooth(method = "gam", formula = y ~ s(x, k = 3))

# with `::`
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  geom_smooth(method = "gam", formula = y ~ mgcv::s(x, k = 3))
#> Warning: Computation failed in `stat_smooth()`:
#> invalid type (list) for variable 'mgcv::s(x, k = 3)'

# this indeed a function from `mgcv package`
args(mgcv::s)
#> function (..., k = -1, fx = FALSE, bs = "tp", m = NA, by = NA, 
#>     xt = NULL, id = NULL, sp = NULL, pc = NULL) 
#> NULL

Created on 2018-08-28 by the reprex package (v0.2.0.9000).

Activity

  1. clauswilke commented on Aug 29, 2018

    @clauswilke
    Member

    This is an mgcv bug. If you try to fit a gam model outside of ggplot2 you'll see that you'll get the same error. See #2621 for a prior discussion on this issue.

    library(mgcv)
    #> Loading required package: nlme
    #> This is mgcv 1.8-24. For overview type 'help("mgcv-package")'.
    x <- rnorm(50)
    y <- x^2 + 0.3*rnorm(50)
    df <- data.frame(x, y)
    gam(y ~ s(x), data = df)
    #> 
    #> Family: gaussian 
    #> Link function: identity 
    #> 
    #> Formula:
    #> y ~ s(x)
    #> 
    #> Estimated degrees of freedom:
    #> 5.22  total = 6.22 
    #> 
    #> GCV score: 0.05352554
    gam(y ~ mgcv::s(x), data = df)
    #> Error in model.frame.default(formula = y ~ mgcv::s(x), data = df, drop.unused.levels = TRUE): invalid type (list) for variable 'mgcv::s(x)'

    Created on 2018-08-28 by the reprex package (v0.2.0).

  2. lock commented on Feb 25, 2019

    @lock

    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/

  3. locked and limited conversation to collaborators on Feb 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions