Skip to content
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

Continuous limits for discrete scales #6273

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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 NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* `scale_{x/y}_discrete(continuous.limits)` is a new argument to control the
display range of discrete scales (@teunbrand, #4174, #6259).
* `geom_ribbon()` now appropriately warns about, and removes, missing values
(@teunbrand, #6243).
* `guide_*()` can now accept two inside legend theme elements:
Expand Down
15 changes: 13 additions & 2 deletions R/scale-discrete-.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#' argument (the number of levels in the scale) returns the numerical values
#' that they should take.
#' @param sec.axis [dup_axis()] is used to specify a secondary axis.
#' @param continuous.limits One of:
#' * `NULL` to use the default scale range
#' * A numeric vector of length two providing a display range for the scale.
#' * A function that accepts the existing continuous limits and returns a
#' numeric vector of length two.
#' @rdname scale_discrete
#' @family position scales
#' @seealso
Expand Down Expand Up @@ -69,7 +74,8 @@
#' }
scale_x_discrete <- function(name = waiver(), ..., palette = seq_len,
expand = waiver(), guide = waiver(),
position = "bottom", sec.axis = waiver()) {
position = "bottom", sec.axis = waiver(),
continuous.limits = NULL) {
sc <- discrete_scale(
aesthetics = ggplot_global$x_aes, name = name,
palette = palette, ...,
Expand All @@ -78,13 +84,15 @@ scale_x_discrete <- function(name = waiver(), ..., palette = seq_len,
)

sc$range_c <- ContinuousRange$new()
sc$continuous_limits <- continuous.limits
set_sec_axis(sec.axis, sc)
}
#' @rdname scale_discrete
#' @export
scale_y_discrete <- function(name = waiver(), ..., palette = seq_len,
expand = waiver(), guide = waiver(),
position = "left", sec.axis = waiver()) {
position = "left", sec.axis = waiver(),
continuous.limits = NULL) {
sc <- discrete_scale(
aesthetics = ggplot_global$y_aes, name = name,
palette = palette, ...,
Expand All @@ -93,6 +101,7 @@ scale_y_discrete <- function(name = waiver(), ..., palette = seq_len,
)

sc$range_c <- ContinuousRange$new()
sc$continuous_limits <- continuous.limits
set_sec_axis(sec.axis, sc)
}

Expand All @@ -106,6 +115,8 @@ scale_y_discrete <- function(name = waiver(), ..., palette = seq_len,
#' @usage NULL
#' @export
ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
continuous_limits = NULL,

train = function(self, x) {
if (is.discrete(x)) {
self$range$train(x, drop = self$drop, na.rm = !self$na.translate)
Expand Down
23 changes: 18 additions & 5 deletions R/scale-expansion.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ expand_range4 <- function(limits, expand) {

# Calculate separate range expansion for the lower and
# upper range limits, and then combine them into one vector
lower <- expand_range(limits, expand[1], expand[2])[1]
upper <- expand_range(limits, expand[3], expand[4])[2]
c(lower, upper)
expand_range(limits, expand[c(1, 3)], expand[c(2, 4)])
}

#' Calculate the default expansion for a scale
Expand Down Expand Up @@ -153,7 +151,8 @@ expand_limits_scale <- function(scale, expand = expansion(0, 0), limits = waiver
scale$map(limits),
expand,
coord_limits,
range_continuous = scale$range_c$range
range_continuous = scale$range_c$range,
continuous_limits = scale$continuous_limits
)
} else {
# using the inverse transform to resolve the NA value is needed for date/datetime/time
Expand All @@ -170,7 +169,21 @@ expand_limits_continuous <- function(limits, expand = expansion(0, 0), coord_lim
}

expand_limits_discrete <- function(limits, expand = expansion(0, 0), coord_limits = c(NA, NA),
range_continuous = NULL) {
range_continuous = NULL, continuous_limits = NULL) {
if (is.function(continuous_limits)) {
continuous_limits <- continuous_limits(limits)
}
if (!is.null(continuous_limits)) {
if (!anyNA(continuous_limits)) {
continuous_limits <- range(continuous_limits)
}
check_numeric(continuous_limits, arg = "continuous.limits")
check_length(continuous_limits, 2L, arg = "continuous.limits")
missing <- is.na(continuous_limits)
limits <- range(ifelse(missing, limits, continuous_limits))
coord_limits <- range(ifelse(missing, coord_limits, continuous_limits))
}

limit_info <- expand_limits_discrete_trans(
limits,
expand,
Expand Down
14 changes: 12 additions & 2 deletions man/scale_discrete.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-scale-expansion.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ test_that("expand_limits_discrete() can override limits with a both discrete and
expand_limits_discrete(c("one", "two"), coord_limits = c(0, NA), range_continuous = c(1, 2)),
c(0, 2)
)
expect_identical(
expand_limits_discrete(1:2, range_continuous = c(1, 2), continuous_limits = c(0, 3)),
c(0, 3)
)
expect_identical(
expand_limits_discrete(1:2, range_continuous = c(1, 2), continuous_limits = c(NA, 4)),
c(1, 4)
)
expect_identical(
expand_limits_discrete(1:2, range_continuous = c(1, 2), continuous_limits = c(0, NA)),
c(0, 2)
)
expect_identical(
expand_limits_discrete(1:2, range_continuous = c(1, 2), continuous_limits = c(NA_real_, NA_real_)),
c(1, 2)
)
expect_identical(
expand_limits_discrete(1:2, range_continuous = 1:2,
continuous_limits = function(x) x + c(-1, 1)),
c(0, 3)
)
})

test_that("expand_limits_continuous_trans() works with inverted transformations", {
Expand Down
Loading