Skip to content

Commit

Permalink
Adjust expand_range() so zero range scales respect mul and add (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dana Paige Seidel committed Jul 26, 2018
1 parent 4687499 commit ddfb517
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
@@ -1,5 +1,8 @@
# scales 0.5.0.9000

* `expand_range()` arguments `mul` and `add` now affect scales with a range of 0
(@dpseidel, tidyverse/ggplot2#2281).

* `date_breaks()` now supports subsecond intervals (@dpseidel, #85).

* New function `modulus_trans()` implements the modulus transformation for positive
Expand Down
9 changes: 3 additions & 6 deletions R/bounds.r
Expand Up @@ -223,15 +223,12 @@ squish_infinite <- function(x, range = c(0, 1)) {
#' @param add additive constant
#' @param zero_width distance to use if range has zero width
#' @export

expand_range <- function(range, mul = 0, add = 0, zero_width = 1) {
if (is.null(range)) return()

if (zero_range(range)) {
c(range[1] - zero_width / 2, range[1] + zero_width / 2)
} else {
range + c(-1, 1) * (diff(range) * mul + add)
}
width <- if (zero_range(range)) zero_width else diff(range)

range + c(-1, 1) * (width * mul + add)
}

#' Determine if range of vector is close to zero, with a specified tolerance
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-bounds.r
Expand Up @@ -79,3 +79,10 @@ test_that("scaling is possible with logical values", {
expect_equal(rescale(c(FALSE, TRUE)), c(0, 1))
expect_equal(rescale_mid(c(FALSE, TRUE), mid = 0.5), c(0, 1))
})

test_that("expand_range respects mul and add values", {
expect_equal(expand_range(c(1,1), mul = 0, add = 0.6), c(0.4, 1.6))
expect_equal(expand_range(c(1,1), mul = 1, add = 0.6), c(-0.6, 2.6))
expect_equal(expand_range(c(1,9), mul = 0, add = 2), c(-1, 11))
})

0 comments on commit ddfb517

Please sign in to comment.