Skip to content

Preserve NAs in discrete palettes #5949

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 4 commits into from
Jul 12, 2024
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* Missing values from discrete palettes are no longer translated
(@teunbrand, #5929).
* Fixed bug in `facet_grid(margins = TRUE)` when using expresssions
(@teunbrand, #1864).
* `geom_step()` now supports the `orientation` argument (@teunbrand, #5936).
Expand Down
24 changes: 12 additions & 12 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -973,23 +973,23 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
self$n.breaks.cache <- n
}

if (!is_null(names(pal))) {
na_value <- if (self$na.translate) self$na.value else NA
pal_names <- names(pal)

if (!is_null(pal_names)) {
# if pal is named, limit the pal by the names first,
# then limit the values by the pal
idx_nomatch <- is.na(match(names(pal), limits))
pal[idx_nomatch] <- NA
pal_match <- pal[match(as.character(x), names(pal))]
pal_match <- unname(pal_match)
} else {
# if pal is not named, limit the values directly
pal_match <- pal[match(as.character(x), limits)]
pal[is.na(match(pal_names, limits))] <- na_value
pal <- unname(pal)
limits <- pal_names
}
pal <- c(pal, na_value)
pal_match <- pal[match(as.character(x), limits, nomatch = length(pal))]
Comment on lines +986 to +987
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is the trick to discrimating (2) and (3). In case (2) we get nomatch'ed to the last palette value, which we set to the na.value. This leaves (3) as-is.


if (self$na.translate) {
ifelse(is.na(x) | is.na(pal_match), self$na.value, pal_match)
} else {
pal_match
if (!is.na(na_value)) {
pal_match[is.na(x)] <- na_value
Comment on lines +989 to +990
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need to translate is.na(pal_match) because we do so through the (lack of) matching above

}
pal_match
},

rescale = function(self, x, limits = self$get_limits(), range = c(1, length(limits))) {
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-scale-manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,27 @@ test_that("limits and breaks (#4619)", {
expect_equal(s3$map(c("4", "6", "8")), c("a", "b", "c"))
expect_equal(s3$break_positions(), c("a", "c"))
})

test_that("NAs from palette are not translated (#5929)", {

s1 <- scale_colour_manual(
values = c("4" = "a", "6" = NA, "8" = "c"),
na.translate = TRUE, na.value = "x"
)
s1$train(c("8", "6", "4"))
expect_equal(s1$map(c("4", "6", "8", "10")), c("a", NA, "c", "x"))

s2 <- scale_colour_manual(
values = c("4" = "a", "6" = NA, "8" = "c"),
na.translate = TRUE, na.value = NA
)
s2$train(c("8", "6", "4"))
expect_equal(s2$map(c("4", "6", "8", "10")), c("a", NA, "c", NA))

s3 <- scale_colour_manual(
values = c("4" = "a", "6" = NA, "8" = "c"),
na.translate = FALSE, na.value = "x"
)
s3$train(c("8", "6", "4"))
expect_equal(s3$map(c("4", "6", "8", "10")), c("a", NA, "c", NA))
})
Loading