Skip to content

Commit

Permalink
fix bug in label_number when scale_cut argument is provided (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Douglas authored and Colin Douglas committed Feb 7, 2024
1 parent 84560bf commit c0f79d3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ scale_cut <- function(x, breaks, scale = 1, accuracy = NULL, suffix = "") {
if (any(bad_break)) {
# If the break below result in a perfect cut, prefer it
lower_break <- breaks[match(break_suffix[bad_break], names(breaks)) - 1]
lower_break[lower_break == 0] <- 1 # Avoid choosing a non-existent break
improved_break <- (x[bad_break] * scale / lower_break) %% 1 == 0
# Unless the break below is a power of 10 change (1.25 is as good as 1250)
power10_break <- log10(breaks[break_suffix[bad_break]] / lower_break) %% 1 == 0
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-label-number.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ test_that("scale_cut prefers clean cuts", {
x <- c(518400, 691200)
# prefers days over week in second element
expect_equal(number(x, scale_cut = cut_time_scale()), c("6d", "8d"))

# do not select off-scale breaks
x <- c(0, 500, 1500, 2000, 2500)
expect_equal(number(x, scale_cut = cut_short_scale()), c("0", "500", "1.5K", "2.0K", "2.5K"))

})

test_that("built-in functions return expected values", {
Expand Down

0 comments on commit c0f79d3

Please sign in to comment.