Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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).
* `geom_rug()` prints a warning when `na.rm = FALSE`, as per documentation (@pn317, #5905)
* `position_dodge(preserve = "single")` now handles multi-row geoms better,
such as `geom_violin()` (@teunbrand based on @clauswilke's work, #2801).
Expand Down
24 changes: 12 additions & 12 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -969,23 +969,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))
})