Skip to content

NA in manual scale with named values #5288

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 5 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ggplot2 (development version)

* Legends in `scale_*_manual()` can show `NA` values again when the `values` is
a named vector (@teunbrand, #5214, #5286).

* `scale_*_manual()` with a named `values` argument now emits a warning when
none of those names match the values found in the data (@teunbrand, #5298).

* `geom_text()` and `geom_label()` gained a `size.unit` parameter that set the
text size to millimetres, points, centimetres, inches or picas
(@teunbrand, #3799).
Expand Down
15 changes: 13 additions & 2 deletions R/scale-manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ scale_discrete_manual <- function(aesthetics, ..., values, breaks = waiver()) {
manual_scale(aesthetics, values, breaks, ...)
}

manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ..., limits = NULL) {
manual_scale <- function(aesthetic, values = NULL, breaks = waiver(), ...,
limits = NULL) {
# check for missing `values` parameter, in lieu of providing
# a default to all the different scale_*_manual() functions
if (is_missing(values)) {
Expand All @@ -152,7 +153,17 @@ 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)) %||% character()
force(aesthetic)
limits <- function(x) {
x <- intersect(x, c(names(values), NA)) %||% character()
if (length(x) < 1) {
cli::cli_warn(paste0(
"No shared levels found between {.code names(values)} of the manual ",
"scale and the data's {.field {aesthetic}} values."
))
}
x
}
}

# order values according to breaks
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/scale-manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# names of values used in manual scales

No shared levels found between `names(values)` of the manual scale and the data's colour values.

13 changes: 13 additions & 0 deletions tests/testthat/test-scale-manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ test_that("names of values used in manual scales", {
s2 <- scale_colour_manual(values = c("8" = "c", "4" = "a", "6" = "b"), na.value = NA)
s2$train(c("4", "8"))
expect_equal(s2$map(c("4", "6", "8")), c("a", NA, "c"))
expect_equal(s2$get_limits(), c("4", "8"))

s3 <- scale_colour_manual(values = c("8" = "c", "4" = "a", "6" = "b"), na.value = "x")
s3$train(c("4", "8", NA))
expect_equal(s3$map(c("4", "6", "8")), c("a", "x", "c"))
expect_equal(s3$get_limits(), c("4", "8", NA))

# Names do not match data
s <- scale_colour_manual(values = c("foo" = "x", "bar" = "y"))
s$train(c("A", "B"))
expect_snapshot_warning(
expect_equal(s$get_limits(), character())
)
})


Expand Down