Skip to content

Commit 0ee259c

Browse files
bfgray3thomasp85
authored andcommitted
default formula argument to NULL in geom_smooth() (#3307)
1 parent fa000f7 commit 0ee259c

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

R/geom-smooth.r

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
geom_smooth <- function(mapping = NULL, data = NULL,
7979
stat = "smooth", position = "identity",
8080
...,
81-
method = "auto",
82-
formula = y ~ x,
81+
method = NULL,
82+
formula = NULL,
8383
se = TRUE,
8484
na.rm = FALSE,
8585
show.legend = NA,

R/stat-smooth.r

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
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
@@ -37,8 +41,8 @@
3741
stat_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
7882
StatSmooth <- 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) {

man/geom_smooth.Rd

+12-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geom-smooth.R

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ test_that("default smoothing methods for small and large data sets work", {
4848
"method = 'gam' and formula 'y ~ s\\(x, bs = \"cs\"\\)"
4949
)
5050
expect_equal(plot_data$y, as.numeric(out))
51+
52+
# backwards compatibility of method = "auto"
53+
p <- ggplot(df, aes(x, y)) + geom_smooth(method = "auto")
54+
55+
expect_message(
56+
plot_data <- layer_data(p),
57+
"method = 'gam' and formula 'y ~ s\\(x, bs = \"cs\"\\)"
58+
)
59+
expect_equal(plot_data$y, as.numeric(out))
5160
})
5261

5362

0 commit comments

Comments
 (0)