Skip to content

Commit 508e1bc

Browse files
committed
default formula argument to NULL in geom_smooth(); closes #3205
1 parent 3b69172 commit 508e1bc

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ core developer team.
6666

6767
## Minor improvements and bug fixes
6868

69+
* The `formula` argument to `geom_smooth()` now defaults to `NULL` and is filled in with the approprate formula in the function body (@bfgray3, #3205).
70+
6971
* `cut_width()` now accepts `...` to pass further arguments to `base::cut.default()`
7072
like `cut_number()` and `cut_interval()` already did (@cderv, #3055)
7173

R/geom-smooth.r

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

R/stat-smooth.r

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#' model that `method = "auto"` would use, then set
1414
#' `method = "gam", formula = y ~ s(x, bs = "cs")`.
1515
#' @param formula Formula to use in smoothing function, eg. `y ~ x`,
16-
#' `y ~ poly(x, 2)`, `y ~ log(x)`
16+
#' `y ~ poly(x, 2)`, `y ~ log(x)`. `NULL` by default, in which case
17+
#' `method = "auto"` implies `formula = y ~ x` when there are fewer than 1,000
18+
#' observations and `formula = y ~ s(x, bs = "cs")` otherwise.
1719
#' @param se Display confidence interval around smooth? (`TRUE` by default, see
1820
#' `level` to control.)
1921
#' @param fullrange Should the fit span the full range of the plot, or just
@@ -38,7 +40,7 @@ stat_smooth <- function(mapping = NULL, data = NULL,
3840
geom = "smooth", position = "identity",
3941
...,
4042
method = "auto",
41-
formula = y ~ x,
43+
formula = NULL,
4244
se = TRUE,
4345
n = 80,
4446
span = 0.75,
@@ -86,9 +88,10 @@ StatSmooth <- ggproto("StatSmooth", Stat,
8688

8789
if (max_group < 1000) {
8890
params$method <- "loess"
91+
params$formula <- params$formula %||% (y ~ x)
8992
} else {
9093
params$method <- "gam"
91-
params$formula <- y ~ s(x, bs = "cs")
94+
params$formula <- params$formula %||% (y ~ s(x, bs = "cs"))
9295
}
9396
message("`geom_smooth()` using method = '", params$method,
9497
"' and formula '", deparse(params$formula), "'")
@@ -100,7 +103,7 @@ StatSmooth <- ggproto("StatSmooth", Stat,
100103
params
101104
},
102105

103-
compute_group = function(data, scales, method = "auto", formula = y~x,
106+
compute_group = function(data, scales, method = "auto", formula = NULL,
104107
se = TRUE, n = 80, span = 0.75, fullrange = FALSE,
105108
xseq = NULL, level = 0.95, method.args = list(),
106109
na.rm = FALSE) {

man/geom_smooth.Rd

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

0 commit comments

Comments
 (0)