1- # ' @param method Smoothing method (function) to use, accepts either a character vector,
2- # ' e.g. `"auto"`, `"lm"`, `"glm"`, `"gam"`, `"loess"` or a function, e.g.
3- # ' `MASS::rlm` or `mgcv::gam`, `stats::lm`, or `stats::loess`.
1+ # ' @param method Smoothing method (function) to use, accepts either
2+ # ' `NULL` or a character vector, e.g. `"lm"`, `"glm"`, `"gam"`, `"loess"`
3+ # ' or a function, e.g. `MASS::rlm` or `mgcv::gam`, `stats::lm`, or `stats::loess`.
4+ # ' `"auto"` is also accepted for backwards compatibility. It is equivalent to
5+ # ' `NULL`.
46# '
5- # ' For `method = "auto" ` the smoothing method is chosen based on the
7+ # ' For `method = NULL ` the smoothing method is chosen based on the
68# ' size of the largest group (across all panels). [stats::loess()] is
79# ' used for less than 1,000 observations; otherwise [mgcv::gam()] is
810# ' used with `formula = y ~ s(x, bs = "cs")` with `method = "REML"`. Somewhat anecdotally,
911# ' `loess` gives a better appearance, but is \eqn{O(N^{2})}{O(N^2)} in memory,
1012# ' so does not work for larger datasets.
1113# '
1214# ' If you have fewer than 1,000 observations but want to use the same `gam()`
13- # ' model that `method = "auto" ` would use, then set
15+ # ' model that `method = NULL ` would use, then set
1416# ' `method = "gam", formula = y ~ s(x, bs = "cs")`.
1517# ' @param formula Formula to use in smoothing function, eg. `y ~ x`,
16- # ' `y ~ poly(x, 2)`, `y ~ log(x)`
18+ # ' `y ~ poly(x, 2)`, `y ~ log(x)`. `NULL` by default, in which case
19+ # ' `method = NULL` implies `formula = y ~ x` when there are fewer than 1,000
20+ # ' observations and `formula = y ~ s(x, bs = "cs")` otherwise.
1721# ' @param se Display confidence interval around smooth? (`TRUE` by default, see
1822# ' `level` to control.)
1923# ' @param fullrange Should the fit span the full range of the plot, or just
3741stat_smooth <- function (mapping = NULL , data = NULL ,
3842 geom = " smooth" , position = " identity" ,
3943 ... ,
40- method = " auto " ,
41- formula = y ~ x ,
44+ method = NULL ,
45+ formula = NULL ,
4246 se = TRUE ,
4347 n = 80 ,
4448 span = 0.75 ,
@@ -77,7 +81,8 @@ stat_smooth <- function(mapping = NULL, data = NULL,
7781# ' @export
7882StatSmooth <- ggproto(" StatSmooth" , Stat ,
7983 setup_params = function (data , params ) {
80- if (identical(params $ method , " auto" )) {
84+ msg <- character ()
85+ if (is.null(params $ method ) || identical(params $ method , " auto" )) {
8186 # Use loess for small datasets, gam with a cubic regression basis for
8287 # larger. Based on size of the _largest_ group to avoid bad memory
8388 # behaviour of loess
@@ -87,18 +92,30 @@ StatSmooth <- ggproto("StatSmooth", Stat,
8792 params $ method <- " loess"
8893 } else {
8994 params $ method <- " gam"
95+ }
96+ msg <- c(msg , paste0(" method = '" , params $ method , " '" ))
97+ }
98+
99+ if (is.null(params $ formula )) {
100+ if (identical(params $ method , " gam" )) {
90101 params $ formula <- y ~ s(x , bs = " cs" )
102+ } else {
103+ params $ formula <- y ~ x
91104 }
92- message(
93- " `geom_smooth()` using method = '" , params $ method ,
94- " ' and formula '" , deparse(params $ formula ), " '"
95- )
105+ msg <- c(msg , paste0(" formula '" , deparse(params $ formula ), " '" ))
106+ }
107+ if (identical(params $ method , " gam" )) {
108+ params $ method <- mgcv :: gam
109+ }
110+
111+ if (length(msg ) > 0 ) {
112+ message(" `geom_smooth()` using " , paste0(msg , collapse = " and " ))
96113 }
97114
98115 params
99116 },
100117
101- compute_group = function (data , scales , method = " auto " , formula = y ~ x ,
118+ compute_group = function (data , scales , method = NULL , formula = NULL ,
102119 se = TRUE , n = 80 , span = 0.75 , fullrange = FALSE ,
103120 xseq = NULL , level = 0.95 , method.args = list (),
104121 na.rm = FALSE ) {
0 commit comments