Skip to content

Commit 54de616

Browse files
clauswilkehadley
authored andcommitted
fix geom_smooth() and add regression tests. Closes #2621. (#2626)
* fix geom_smooth() and add regression tests. Closes #2621. * tidyverse style * fix news
1 parent 2a5e494 commit 54de616

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

NEWS.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,8 @@
343343
plotting thick arrows (@Ax3man, #774).
344344

345345
* `geom_smooth()` now reports the formula used when `method = "auto"`
346-
(@davharris #1951). When mgcv is used, the `s` in `y ~ s(x, bs = "cs")`
347-
is now fully qualified as `mgcv::s()` to avoid name conflicts (@bfgray3,
348-
#2535). `geom_smooth()` now orders by the `x` aesthetic, making it easier
349-
to pass pre-computed values without manual ordering (@izahn, #2028). It
346+
(@davharris #1951). `geom_smooth()` now orders by the `x` aesthetic, making it
347+
easier to pass pre-computed values without manual ordering (@izahn, #2028). It
350348
also now knows it has `ymin` and `ymax` aesthetics (#1939). The legend
351349
correctly reflects the status of the `se` argument when used with stats
352350
other than the default (@clauswilke, #1546).

R/stat-smooth.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ StatSmooth <- ggproto("StatSmooth", Stat,
8383
params$method <- "loess"
8484
} else {
8585
params$method <- "gam"
86-
params$formula <- y ~ mgcv::s(x, bs = "cs")
86+
params$formula <- y ~ s(x, bs = "cs")
8787
}
88-
message("`geom_smooth()` using method = '", params$method,
88+
message("`geom_smooth()` using method = '", params$method,
8989
"' and formula '", deparse(params$formula), "'")
9090
}
9191
if (identical(params$method, "gam")) {

tests/testthat/test-geom-smooth.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,48 @@ test_that("Data is ordered by x", {
99
expect_equal(layer_data(ps)[c("x", "y")], df[order(df$x), ])
1010
})
1111

12+
test_that("Default smoothing methods for small and large data sets work", {
13+
# test small data set
14+
set.seed(6531)
15+
x <- rnorm(10)
16+
df <- data.frame(
17+
x = x,
18+
y = x^2 + 0.5 * rnorm(10)
19+
)
20+
21+
m <- loess(y ~ x, data = df, span = 0.75)
22+
range <- range(df$x, na.rm = TRUE)
23+
xseq <- seq(range[1], range[2], length.out = 80)
24+
out <- predict(m, data.frame(x = xseq))
25+
p <- ggplot(df, aes(x, y)) + geom_smooth()
26+
27+
expect_message(
28+
plot_data <- layer_data(p),
29+
"method = 'loess' and formula 'y ~ x'"
30+
)
31+
expect_equal(plot_data$y, as.numeric(out))
32+
33+
# test large data set
34+
x <- rnorm(1001) # 1000 is the cutoff point for gam
35+
df <- data.frame(
36+
x = x,
37+
y = x^2 + 0.5 * rnorm(1001)
38+
)
39+
40+
m <- mgcv::gam(y ~ s(x, bs = "cs"), data = df)
41+
range <- range(df$x, na.rm = TRUE)
42+
xseq <- seq(range[1], range[2], length.out = 80)
43+
out <- predict(m, data.frame(x = xseq))
44+
p <- ggplot(df, aes(x, y)) + geom_smooth()
45+
46+
expect_message(
47+
plot_data <- layer_data(p),
48+
"method = 'gam' and formula 'y ~ s\\(x, bs = \"cs\"\\)"
49+
)
50+
expect_equal(plot_data$y, as.numeric(out))
51+
52+
})
53+
1254
# Visual tests ------------------------------------------------------------
1355

1456
test_that("geom_smooth() works with alternative stats", {

0 commit comments

Comments
 (0)