From fe5953e5b724b991c9172ae5bf61719de9d52eba Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Wed, 15 Sep 2021 22:25:20 +0200 Subject: [PATCH 1/8] manual scale limits default to present names of values --- R/scale-manual.r | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/scale-manual.r b/R/scale-manual.r index 6e408e4c12..a5fd16b77e 100644 --- a/R/scale-manual.r +++ b/R/scale-manual.r @@ -132,8 +132,9 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit force(values) } - if (is.null(limits)) { - limits <- names(values) + if (is.null(limits) && !is.null(names(values))) { + force(values) + limits <- function(x) {intersect(x, names(values))} } # order values according to breaks From fa042fc0ab9ab29d86ff3c7fab54225c4b06a3a0 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Thu, 16 Sep 2021 16:23:44 +0200 Subject: [PATCH 2/8] Don't force values (again) --- R/scale-manual.r | 1 - 1 file changed, 1 deletion(-) diff --git a/R/scale-manual.r b/R/scale-manual.r index a5fd16b77e..3a2dd719ae 100644 --- a/R/scale-manual.r +++ b/R/scale-manual.r @@ -133,7 +133,6 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit } if (is.null(limits) && !is.null(names(values))) { - force(values) limits <- function(x) {intersect(x, names(values))} } From ba530046902f45fa567d231ea1f71122d58a6673 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sat, 18 Sep 2021 23:07:31 +0200 Subject: [PATCH 3/8] Add news bullet --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index 4a5398a300..67b48e8b54 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,11 @@ # ggplot2 (development version) +* `scale_*_manual()` no longer displays extra legend keys, or changes their + order, when a named `values` argument has more items than the data. The + previous behaviour can be replicated by using + `scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai, + #4511, #4534, #4556, #4545, #4616) + # ggplot2 3.3.5 This is a very small release focusing on fixing a couple of untenable issues that surfaced with the 3.3.4 release From c0877d76ff30de1c37dc5fdd2e8b6f2ea658bb8c Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 19 Sep 2021 09:04:22 +0200 Subject: [PATCH 4/8] Trim mentioned issues Co-authored-by: Hiroaki Yutani --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 67b48e8b54..1c8d50fb1d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,7 +4,7 @@ order, when a named `values` argument has more items than the data. The previous behaviour can be replicated by using `scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai, - #4511, #4534, #4556, #4545, #4616) + #4511, #4534) # ggplot2 3.3.5 This is a very small release focusing on fixing a couple of untenable issues From 4635800c35d9e6bf63639574853d3812447deef0 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 19 Sep 2021 09:04:51 +0200 Subject: [PATCH 5/8] Omit brackets Co-authored-by: Hiroaki Yutani --- R/scale-manual.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/scale-manual.r b/R/scale-manual.r index 3a2dd719ae..3f9fb73429 100644 --- a/R/scale-manual.r +++ b/R/scale-manual.r @@ -133,7 +133,7 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit } if (is.null(limits) && !is.null(names(values))) { - limits <- function(x) {intersect(x, names(values))} + limits <- function(x) intersect(x, names(values)) } # order values according to breaks From 36713290783ce30b632212f43d6faee69103b054 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 19 Sep 2021 09:08:48 +0200 Subject: [PATCH 6/8] Add small comment --- R/scale-manual.r | 1 + 1 file changed, 1 insertion(+) diff --git a/R/scale-manual.r b/R/scale-manual.r index 3f9fb73429..28d35dc57b 100644 --- a/R/scale-manual.r +++ b/R/scale-manual.r @@ -133,6 +133,7 @@ manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limit } if (is.null(limits) && !is.null(names(values))) { + # Limits as function to access `values` names later on (#4619) limits <- function(x) intersect(x, names(values)) } From 3027023897d6cd75d85f687dba02078cfc579605 Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Sun, 19 Sep 2021 09:31:22 +0200 Subject: [PATCH 7/8] Remove trailing comma from test --- tests/testthat/test-scale-manual.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-scale-manual.r b/tests/testthat/test-scale-manual.r index d33fe17da8..9355558893 100644 --- a/tests/testthat/test-scale-manual.r +++ b/tests/testthat/test-scale-manual.r @@ -117,7 +117,7 @@ test_that("fewer values (#3451)", { test_that("limits and breaks (#4619)", { # values don't change legend order s1 <- scale_colour_manual( - values = c("8" = "c", "4" = "a", "6" = "b"), + values = c("8" = "c", "4" = "a", "6" = "b") ) s1$train(c("8", "6", "4")) expect_equal(s1$map(c("8", "6", "4")), c("c", "b", "a")) From 91bad650b462c0e7da324ca4fb0e4788389af95a Mon Sep 17 00:00:00 2001 From: Teun van den Brand <49372158+teunbrand@users.noreply.github.com> Date: Mon, 20 Sep 2021 07:44:56 +0200 Subject: [PATCH 8/8] Rephrase news bullet --- NEWS.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1c8d50fb1d..9364b75a31 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,8 +1,8 @@ # ggplot2 (development version) * `scale_*_manual()` no longer displays extra legend keys, or changes their - order, when a named `values` argument has more items than the data. The - previous behaviour can be replicated by using + order, when a named `values` argument has more items than the data. To display + all `values` on the legend instead, use `scale_*_manual(values = vals, limits = names(vals))`. (@teunbrand, @banfai, #4511, #4534)