diff --git a/NEWS.md b/NEWS.md index fd768ee1..85eb828c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,8 @@ vectors during training. Mixing of character and factor data will still lead to different results depending on the training order. * Add a rescale method for `difftime` objects (#382) +* The `scale_cut` argument in `number()` now works as advertised for values + below the lowest cut value (#346) # scales 1.2.1 diff --git a/R/label-number.R b/R/label-number.R index 0e8e1a2e..5ae60123 100644 --- a/R/label-number.R +++ b/R/label-number.R @@ -327,9 +327,6 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") { if (any(is.na(breaks))) { cli::cli_abort("{.arg scale_cut} values must not be missing") } - if (!identical(breaks[[1]], 0) && !identical(breaks[[1]], 0L)) { - cli::cli_abort("Smallest value of {.arg scales_cut} must be zero") - } break_suffix <- as.character(cut( abs(x * scale), @@ -337,14 +334,13 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") { labels = c(names(breaks)), right = FALSE )) - break_suffix[is.na(break_suffix)] <- names(which.min(breaks)) + break_suffix[is.na(break_suffix)] <- "" break_scale <- scale * unname(1 / breaks[break_suffix]) break_scale[which(break_scale %in% c(Inf, NA))] <- scale - # exact zero is not scaled - x_zero <- which(abs(x) == 0) - scale[x_zero] <- 1 + # exact zero is not scaled, nor are values below lowest break + break_scale[abs(x) == 0 | is.na(break_scale)] <- 1 suffix <- paste0(break_suffix, suffix) accuracy <- accuracy %||% stats::ave(x * break_scale, break_scale, FUN = precision) diff --git a/tests/testthat/_snaps/label-number.md b/tests/testthat/_snaps/label-number.md index 49fdbe97..c70d7aa6 100644 --- a/tests/testthat/_snaps/label-number.md +++ b/tests/testthat/_snaps/label-number.md @@ -10,11 +10,6 @@ Condition Error in `scale_cut()`: ! `scale_cut` must be a named numeric vector - Code - number(1, scale_cut = c(x = 1, y = 2)) - Condition - Error in `scale_cut()`: - ! Smallest value of `scales_cut` must be zero Code number(1, scale_cut = c(x = 0, NA)) Condition diff --git a/tests/testthat/test-label-number.R b/tests/testthat/test-label-number.R index 60b11522..2f4a9399 100644 --- a/tests/testthat/test-label-number.R +++ b/tests/testthat/test-label-number.R @@ -171,7 +171,6 @@ test_that("scale_cut checks its inputs", { expect_snapshot(error = TRUE, { number(1, scale_cut = 0) number(1, scale_cut = "x") - number(1, scale_cut = c(x = 1, y = 2)) number(1, scale_cut = c(x = 0, NA)) }) })