Skip to content

Improve pal_qualitative() #5954

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
Aug 20, 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
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)

* (internal) improvements to `pal_qualitative()` (@teunbrand, #5013)
* `coord_radial(clip = "on")` clips to the panel area when the graphics device
supports clipping paths (@teunbrand, #5952).
* (internal) Panel clipping responsibility moved from Facet class to Coord
Expand Down
22 changes: 11 additions & 11 deletions R/scale-hue.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,22 @@ scale_fill_qualitative <- function(name = waiver(), ..., type = NULL,
#' @param type a character vector or a list of character vectors
#' @noRd
pal_qualitative <- function(type, h, c, l, h.start, direction) {
type_list <- type
if (!is.list(type_list)) {
type_list <- list(type_list)
}
if (!all(vapply(type_list, is.character, logical(1)))) {
stop_input_type(type, "a character vector or list of character vectors")
}
type_lengths <- lengths(type_list)
function(n) {
type_list <- if (!is.list(type)) list(type) else type
if (!all(vapply(type_list, is.character, logical(1)))) {
cli::cli_abort("{.arg type} must be a character vector or a list of character vectors.")
}
type_lengths <- lengths(type_list)
# If there are more levels than color codes default to pal_hue()
if (max(type_lengths) < n) {
return(scales::pal_hue(h, c, l, h.start, direction)(n))
}
# Use the minimum length vector that exceeds the number of levels (n)
type_list <- type_list[order(type_lengths)]
i <- 1
while (length(type_list[[i]]) < n) {
i <- i + 1
}
type_list[[i]][seq_len(n)]
i <- which(type_lengths >= n)
i <- i[which.min(type_lengths[i])]
type_list[[i]]
}
}
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/scale-hue.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# scale_hue() checks the type input

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

3 changes: 1 addition & 2 deletions tests/testthat/test-scale-hue.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
test_that("scale_hue() checks the type input", {
pal <- pal_qualitative(type = 1:4)
expect_snapshot_error(pal(4))
expect_snapshot_error(pal_qualitative(type = 1:4))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

error is now thrown in palette factory rather than the output function.

pal <- pal_qualitative(type = colors())
expect_silent(pal(4))
pal <- pal_qualitative(type = list(colors()[1:10], colors()[11:30]))
Expand Down
Loading