diff --git a/NEWS.md b/NEWS.md index 140a9df61a..f96283e156 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # ggplot2 (development version) + +* Make sure that default labels from default mappings doesn't overwrite default + labels from explicit mappings (@thomasp85, #2406) + * `stat_count()` now computes width based on the full dataset instead of per group (@thomasp85, #2047) diff --git a/R/plot-construction.r b/R/plot-construction.r index 6248c4914e..2c8c7c2e28 100644 --- a/R/plot-construction.r +++ b/R/plot-construction.r @@ -167,8 +167,16 @@ ggplot_add.Layer <- function(object, plot, object_name) { # Add any new labels mapping <- make_labels(object$mapping) - default <- make_labels(object$stat$default_aes) + default <- lapply(make_labels(object$stat$default_aes), function(l) { + attr(l, "fallback") <- TRUE + l + }) new_labels <- defaults(mapping, default) - plot$labels <- defaults(plot$labels, new_labels) + current_labels <- plot$labels + current_fallbacks <- vapply(current_labels, function(l) isTRUE(attr(l, "fallback")), logical(1)) + plot$labels <- defaults(current_labels[!current_fallbacks], new_labels) + if (any(current_fallbacks)) { + plot$labels <- defaults(plot$labels, current_labels) + } plot } diff --git a/tests/testthat/test-labels.r b/tests/testthat/test-labels.r index dd13f07f78..b760b393e7 100644 --- a/tests/testthat/test-labels.r +++ b/tests/testthat/test-labels.r @@ -50,6 +50,18 @@ test_that("setting guide labels works", { ) }) +test_that("Labels from default stat mapping are overwritten by default labels", { + p <- ggplot(mpg, aes(displ, hwy)) + + geom_density2d() + + expect_equal(p$labels$colour[1], "colour") + expect_true(attr(p$labels$colour, "fallback")) + + p <- p + geom_smooth(aes(color = drv)) + + expect_equal(p$labels$colour, "drv") +}) + # Visual tests ------------------------------------------------------------