Skip to content

Fix issue 1546, fill aesthetic does not show in the legend guide for stat_summary #2567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ up correct aspect ratio, and draws a graticule.
(@davharris #1951). It also orders by the `x` aesthetic, making it easier
to pass pre-computed values without manual ordering (@izahn, #2028).
It also now knows it has `ymin` and `ymax` aesthetics (#1939).

* Fixed legend-drawing bug when using `geom_smooth()` with stats other than
`stat_smooth()` (@clauswilke, #1546).

* `geom_tile()` now once again interprets `width` and `height` correctly
(@malcolmbarrett, #2510)
Expand Down
14 changes: 11 additions & 3 deletions R/geom-smooth.r
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ geom_smooth <- function(mapping = NULL, data = NULL,

params <- list(
na.rm = na.rm,
se = se,
...
)
if (identical(stat, "smooth")) {
params$method <- method
params$formula <- formula
params$se <- se
}

layer(
Expand All @@ -115,11 +115,19 @@ GeomSmooth <- ggproto("GeomSmooth", Geom,
setup_data = function(data, params) {
GeomLine$setup_data(data, params)
},
draw_group = function(data, panel_params, coord) {

# The `se` argument is set to false here to make sure drawing the
# geom and drawing the legend is in synch. If the geom is used by a
# stat that doesn't set the `se` argument then `se` will be missing
# and the legend key won't be drawn. With `se = FALSE` here the
# ribbon won't be drawn either in that case, keeping the overall
# behavior predictable and sensible. The user will realize that they
# need to set `se = TRUE` to obtain the ribbon and the legend key.
draw_group = function(data, panel_params, coord, se = FALSE) {
ribbon <- transform(data, colour = NA)
path <- transform(data, alpha = NA)

has_ribbon <- !is.null(data$ymax) && !is.null(data$ymin)
has_ribbon <- se && !is.null(data$ymax) && !is.null(data$ymin)

gList(
if (has_ribbon) GeomRibbon$draw_group(ribbon, panel_params, coord),
Expand Down
61 changes: 61 additions & 0 deletions tests/figs/geom-smooth/ribbon-turned-off-in-geom-smooth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions tests/figs/geom-smooth/ribbon-turned-on-in-geom-smooth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/test-function-args.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("geom_xxx and GeomXxx$draw arg defaults match", {
# These aren't actually geoms, or need special parameters and can't be tested this way.
geom_fun_names <- setdiff(
geom_fun_names,
c("geom_map", "geom_sf", "geom_column", "annotation_custom", "annotation_map",
c("geom_map", "geom_sf", "geom_smooth", "geom_column", "annotation_custom", "annotation_map",
"annotation_raster", "annotation_id")
)

Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-geom-smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ test_that("Data is ordered by x", {

expect_equal(layer_data(ps)[c("x", "y")], df[order(df$x), ])
})

# Visual tests ------------------------------------------------------------

test_that("geom_smooth() works with alternative stats", {
df <- data.frame(x = c(1, 1, 2, 2, 1, 1, 2, 2),
y = c(1, 2, 2, 3, 2, 3, 1, 2),
fill = c(rep("A", 4), rep("B", 4)))

expect_doppelganger("ribbon turned on in geom_smooth", {
ggplot(df, aes(x, y, color = fill, fill = fill)) +
geom_smooth(stat = "summary") # ribbon on by default
})

expect_doppelganger("ribbon turned off in geom_smooth", {
ggplot(df, aes(x, y, color = fill, fill = fill)) +
geom_smooth(stat = "summary", se = FALSE) # ribbon is turned off via `se = FALSE`
})
})