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`.
4
6
# '
5
- # ' For `method = "auto" ` the smoothing method is chosen based on the
7
+ # ' For `method = NULL ` the smoothing method is chosen based on the
6
8
# ' size of the largest group (across all panels). [stats::loess()] is
7
9
# ' used for less than 1,000 observations; otherwise [mgcv::gam()] is
8
10
# ' used with `formula = y ~ s(x, bs = "cs")` with `method = "REML"`. Somewhat anecdotally,
9
11
# ' `loess` gives a better appearance, but is \eqn{O(N^{2})}{O(N^2)} in memory,
10
12
# ' so does not work for larger datasets.
11
13
# '
12
14
# ' 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
14
16
# ' `method = "gam", formula = y ~ s(x, bs = "cs")`.
15
17
# ' @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.
17
21
# ' @param se Display confidence interval around smooth? (`TRUE` by default, see
18
22
# ' `level` to control.)
19
23
# ' @param fullrange Should the fit span the full range of the plot, or just
37
41
stat_smooth <- function (mapping = NULL , data = NULL ,
38
42
geom = " smooth" , position = " identity" ,
39
43
... ,
40
- method = " auto " ,
41
- formula = y ~ x ,
44
+ method = NULL ,
45
+ formula = NULL ,
42
46
se = TRUE ,
43
47
n = 80 ,
44
48
span = 0.75 ,
@@ -77,7 +81,8 @@ stat_smooth <- function(mapping = NULL, data = NULL,
77
81
# ' @export
78
82
StatSmooth <- ggproto(" StatSmooth" , Stat ,
79
83
setup_params = function (data , params ) {
80
- if (identical(params $ method , " auto" )) {
84
+ msg <- character ()
85
+ if (is.null(params $ method ) || identical(params $ method , " auto" )) {
81
86
# Use loess for small datasets, gam with a cubic regression basis for
82
87
# larger. Based on size of the _largest_ group to avoid bad memory
83
88
# behaviour of loess
@@ -87,18 +92,30 @@ StatSmooth <- ggproto("StatSmooth", Stat,
87
92
params $ method <- " loess"
88
93
} else {
89
94
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" )) {
90
101
params $ formula <- y ~ s(x , bs = " cs" )
102
+ } else {
103
+ params $ formula <- y ~ x
91
104
}
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 " ))
96
113
}
97
114
98
115
params
99
116
},
100
117
101
- compute_group = function (data , scales , method = " auto " , formula = y ~ x ,
118
+ compute_group = function (data , scales , method = NULL , formula = NULL ,
102
119
se = TRUE , n = 80 , span = 0.75 , fullrange = FALSE ,
103
120
xseq = NULL , level = 0.95 , method.args = list (),
104
121
na.rm = FALSE ) {
0 commit comments