Skip to content

Make ScaleDiscrete$map() compatible with vctrs-palettes #6118

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
Jan 27, 2025
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ggplot2 (development version)

* More stability for vctrs-based palettes (@teunbrand, #6117).
* Fixed regression in `guide_bins(reverse = TRUE)` (@teunbrand, #6183).
* New function family for setting parts of a theme. For example, you can now use
`theme_sub_axis(line, text, ticks, ticks.length, line)` as a substitute for
Expand Down
29 changes: 19 additions & 10 deletions R/scale-.R
Original file line number Diff line number Diff line change
Expand Up @@ -963,10 +963,10 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
transform = identity,

map = function(self, x, limits = self$get_limits()) {
limits <- limits[!is.na(limits)]
n <- length(limits)
limits <- vec_slice(limits, !is.na(limits))
n <- vec_size(limits)
if (n < 1) {
return(rep(self$na.value, length(x)))
return(vec_rep(self$na.value, vec_size(x)))
}
if (!is.null(self$n.breaks.cache) && self$n.breaks.cache == n) {
pal <- self$palette.cache
Expand All @@ -982,21 +982,30 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale,
self$n.breaks.cache <- n
}

na_value <- if (self$na.translate) self$na.value else NA
pal_names <- names(pal)
na_value <- NA
if (self$na.translate) {
na_value <- self$na.value
if (obj_is_list(pal) && !obj_is_list(na_value)) {
# We prevent a casting error that occurs when mapping grid patterns
na_value <- list(na_value)
}
Comment on lines +988 to +991
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't love this, but I also don't know how else to solve this

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is too bad

}

pal_names <- vec_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
pal[is.na(match(pal_names, limits))] <- na_value
pal <- unname(pal)
vec_slice(pal, is.na(match(pal_names, limits))) <- na_value
pal <- vec_set_names(pal, NULL)
limits <- pal_names
}
pal <- c(pal, na_value)
pal_match <- pal[match(as.character(x), limits, nomatch = length(pal))]
pal <- vec_c(pal, na_value)
pal_match <-
vec_slice(pal, match(as.character(x), limits, nomatch = vec_size(pal)))

if (!is.na(na_value)) {
pal_match[is.na(x)] <- na_value
vec_slice(pal_match, is.na(x)) <- na_value
}
pal_match
},
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-scales.R
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,32 @@ test_that("discrete scales work with NAs in arbitrary positions", {
expect_equal(test, output)

})

test_that("discrete scales can map to 2D structures", {

p <- ggplot(mtcars, aes(disp, mpg, colour = factor(cyl))) +
geom_point()

# Test it can map to a vctrs rcrd class
rcrd <- new_rcrd(list(a = LETTERS[1:3], b = 3:1))

ld <- layer_data(p + scale_colour_manual(values = rcrd, na.value = NA))
expect_s3_class(ld$colour, "vctrs_rcrd")
expect_length(ld$colour, nrow(mtcars))

# Test it can map to data.frames
df <- data_frame0(a = LETTERS[1:3], b = 3:1)
my_pal <- function(n) vec_slice(df, seq_len(n))

ld <- layer_data(p + discrete_scale("colour", palette = my_pal))
expect_s3_class(ld$colour, "data.frame")
expect_equal(dim(ld$colour), c(nrow(mtcars), ncol(df)))

# Test it can map to matrices
mtx <- cbind(a = LETTERS[1:3], b = LETTERS[4:6])
my_pal <- function(n) vec_slice(mtx, seq_len(n))

ld <- layer_data(p + discrete_scale("colour", palette = my_pal))
expect_true(is.matrix(ld$colour))
expect_equal(dim(ld$colour), c(nrow(mtcars), ncol(df)))
})
Loading