Skip to content

Commit 8e365a4

Browse files
clauswilkehadley
authored andcommitted
Fix issue 1546, fill aesthetic does not show in the legend guide for stat_summary (#2567)
* add `se` parameter to geom_smooth() * update news; closes #1546 * disable consistency check for geom_smooth draw arguments, add visual tests
1 parent 46150d7 commit 8e365a4

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ up correct aspect ratio, and draws a graticule.
265265
(@davharris #1951). It also orders by the `x` aesthetic, making it easier
266266
to pass pre-computed values without manual ordering (@izahn, #2028).
267267
It also now knows it has `ymin` and `ymax` aesthetics (#1939).
268+
269+
* Fixed legend-drawing bug when using `geom_smooth()` with stats other than
270+
`stat_smooth()` (@clauswilke, #1546).
268271

269272
* `geom_tile()` now once again interprets `width` and `height` correctly
270273
(@malcolmbarrett, #2510)

R/geom-smooth.r

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ geom_smooth <- function(mapping = NULL, data = NULL,
8787

8888
params <- list(
8989
na.rm = na.rm,
90+
se = se,
9091
...
9192
)
9293
if (identical(stat, "smooth")) {
9394
params$method <- method
9495
params$formula <- formula
95-
params$se <- se
9696
}
9797

9898
layer(
@@ -115,11 +115,19 @@ GeomSmooth <- ggproto("GeomSmooth", Geom,
115115
setup_data = function(data, params) {
116116
GeomLine$setup_data(data, params)
117117
},
118-
draw_group = function(data, panel_params, coord) {
118+
119+
# The `se` argument is set to false here to make sure drawing the
120+
# geom and drawing the legend is in synch. If the geom is used by a
121+
# stat that doesn't set the `se` argument then `se` will be missing
122+
# and the legend key won't be drawn. With `se = FALSE` here the
123+
# ribbon won't be drawn either in that case, keeping the overall
124+
# behavior predictable and sensible. The user will realize that they
125+
# need to set `se = TRUE` to obtain the ribbon and the legend key.
126+
draw_group = function(data, panel_params, coord, se = FALSE) {
119127
ribbon <- transform(data, colour = NA)
120128
path <- transform(data, alpha = NA)
121129

122-
has_ribbon <- !is.null(data$ymax) && !is.null(data$ymin)
130+
has_ribbon <- se && !is.null(data$ymax) && !is.null(data$ymin)
123131

124132
gList(
125133
if (has_ribbon) GeomRibbon$draw_group(ribbon, panel_params, coord),
Lines changed: 61 additions & 0 deletions
Loading
Lines changed: 65 additions & 0 deletions
Loading

tests/testthat/test-function-args.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test_that("geom_xxx and GeomXxx$draw arg defaults match", {
1313
# These aren't actually geoms, or need special parameters and can't be tested this way.
1414
geom_fun_names <- setdiff(
1515
geom_fun_names,
16-
c("geom_map", "geom_sf", "geom_column", "annotation_custom", "annotation_map",
16+
c("geom_map", "geom_sf", "geom_smooth", "geom_column", "annotation_custom", "annotation_map",
1717
"annotation_raster", "annotation_id")
1818
)
1919

tests/testthat/test-geom-smooth.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,21 @@ test_that("Data is ordered by x", {
88

99
expect_equal(layer_data(ps)[c("x", "y")], df[order(df$x), ])
1010
})
11+
12+
# Visual tests ------------------------------------------------------------
13+
14+
test_that("geom_smooth() works with alternative stats", {
15+
df <- data.frame(x = c(1, 1, 2, 2, 1, 1, 2, 2),
16+
y = c(1, 2, 2, 3, 2, 3, 1, 2),
17+
fill = c(rep("A", 4), rep("B", 4)))
18+
19+
expect_doppelganger("ribbon turned on in geom_smooth", {
20+
ggplot(df, aes(x, y, color = fill, fill = fill)) +
21+
geom_smooth(stat = "summary") # ribbon on by default
22+
})
23+
24+
expect_doppelganger("ribbon turned off in geom_smooth", {
25+
ggplot(df, aes(x, y, color = fill, fill = fill)) +
26+
geom_smooth(stat = "summary", se = FALSE) # ribbon is turned off via `se = FALSE`
27+
})
28+
})

0 commit comments

Comments
 (0)