Skip to content

Commit c8b8022

Browse files
authored
Improve pal_qualitative() (#5954)
* precompute checks and lengths * don't use loop to select minimal palette * accept change in error message * add news bullet
1 parent 995b40c commit c8b8022

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# ggplot2 (development version)
22

3+
* (internal) improvements to `pal_qualitative()` (@teunbrand, #5013)
34
* `coord_radial(clip = "on")` clips to the panel area when the graphics device
45
supports clipping paths (@teunbrand, #5952).
56
* (internal) Panel clipping responsibility moved from Facet class to Coord

R/scale-hue.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,22 @@ scale_fill_qualitative <- function(name = waiver(), ..., type = NULL,
205205
#' @param type a character vector or a list of character vectors
206206
#' @noRd
207207
pal_qualitative <- function(type, h, c, l, h.start, direction) {
208+
type_list <- type
209+
if (!is.list(type_list)) {
210+
type_list <- list(type_list)
211+
}
212+
if (!all(vapply(type_list, is.character, logical(1)))) {
213+
stop_input_type(type, "a character vector or list of character vectors")
214+
}
215+
type_lengths <- lengths(type_list)
208216
function(n) {
209-
type_list <- if (!is.list(type)) list(type) else type
210-
if (!all(vapply(type_list, is.character, logical(1)))) {
211-
cli::cli_abort("{.arg type} must be a character vector or a list of character vectors.")
212-
}
213-
type_lengths <- lengths(type_list)
214217
# If there are more levels than color codes default to pal_hue()
215218
if (max(type_lengths) < n) {
216219
return(scales::pal_hue(h, c, l, h.start, direction)(n))
217220
}
218221
# Use the minimum length vector that exceeds the number of levels (n)
219-
type_list <- type_list[order(type_lengths)]
220-
i <- 1
221-
while (length(type_list[[i]]) < n) {
222-
i <- i + 1
223-
}
224-
type_list[[i]][seq_len(n)]
222+
i <- which(type_lengths >= n)
223+
i <- i[which.min(type_lengths[i])]
224+
type_list[[i]]
225225
}
226226
}

tests/testthat/_snaps/scale-hue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# scale_hue() checks the type input
22

3-
`type` must be a character vector or a list of character vectors.
3+
`type` must be a character vector or list of character vectors, not an integer vector.
44

tests/testthat/test-scale-hue.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
test_that("scale_hue() checks the type input", {
2-
pal <- pal_qualitative(type = 1:4)
3-
expect_snapshot_error(pal(4))
2+
expect_snapshot_error(pal_qualitative(type = 1:4))
43
pal <- pal_qualitative(type = colors())
54
expect_silent(pal(4))
65
pal <- pal_qualitative(type = list(colors()[1:10], colors()[11:30]))

0 commit comments

Comments
 (0)