Skip to content
Open
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export(label_scientific)
export(label_time)
export(label_timespan)
export(label_wrap)
export(limits_center)
export(limits_include)
export(linetype_pal)
export(log10_trans)
export(log1p_trans)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* Added colour manipulation functions: `col_shift()`, `col_saturate()`,
`col_darker()`, `col_lighter()` and `col_mix()` (@teunbrand, #423)
* `label_date_short()` gains `tz` and `locale` argument (#478)
* New `limits_center()` and `limits_include()` functions for setting limits
(#490)

# scales 1.3.0

Expand Down
33 changes: 33 additions & 0 deletions R/limits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Limits include value
#'
#' @param value A scalar numeric value to include.
#'
#' @returns The `limits_()` functions return a function for setting limits.
#' These functions take as their argument a vector values that represent the
#' natural data limits.
#' @export
#'
#' @examples
#' # Ensure 0 is included in the scale range
#' demo_continuous(c(5, 10), limits = limits_include(0))
limits_include <- function(value = 0) {
check_number_decimal(value)
function(x) range(x, value)
}

#' Centered limits
#'
#' @param center A scalar numeric value to serve as midpoint.
#'
#' @returns The `limits_()` functions return a function for setting limits.
#' These functions take as their argument a vector values that represent the
#' natural data limits.
#' @export
#'
#' @examples
#' # Symmetry around 0
#' demo_continuous(c(-5, 10), limits = limits_center(0))
limits_center <- function(center = 0) {
check_number_decimal(center)
function(x) c(-1, +1) * max(abs(x - center)) + center
}
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ reference:
- expand_range
- discard
- censor
- limits_center
- limits_include

- title: Transformations
desc: >
Expand Down
23 changes: 23 additions & 0 deletions man/limits_center.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/limits_include.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/testthat/test-limits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("limits_include gives expected results", {
x <- limits_include(2)(c(-5, -1))
expect_equal(x, c(-5, 2))

x <- limits_include(-2)(c(5, 1))
expect_equal(x, c(-2, 5))
})

test_that("limits_center gives expected results", {
x <- limits_center(-1)(c(0, 1))
expect_equal(x, c(-3, 1))

x <- limits_center(1)(c(0, 1))
expect_equal(x, c(0, 2))
})