Skip to content

Fix bug in discrete position scales with continuous limits (fixes #3918) #3919

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

Merged
merged 5 commits into from
Apr 30, 2020
Merged
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
10 changes: 10 additions & 0 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),

check_breaks_labels(breaks, labels)

if (!is.function(limits) && (length(limits) > 0) && !is.discrete(limits)) {
warn(
glue(
"
Continuous limits supplied to discrete scale.
Did you mean `limits = factor(...)` or `scale_*_continuous()`?"
)
)
}

position <- match.arg(position, c("left", "right", "top", "bottom"))

# If the scale is non-positional, break = NULL means removing the guide
Expand Down
12 changes: 8 additions & 4 deletions R/scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,25 @@ expand_limits_continuous_trans <- function(limits, expand = expansion(0, 0),
expand_limits_discrete_trans <- function(limits, expand = expansion(0, 0),
coord_limits = c(NA, NA), trans = identity_trans(),
range_continuous = NULL) {
if (is.discrete(limits)) {
n_discrete_limits <- length(limits)
} else {
n_discrete_limits <- 0
}

n_limits <- length(limits)
is_empty <- is.null(limits) && is.null(range_continuous)
is_only_continuous <- n_limits == 0
is_only_continuous <- n_discrete_limits == 0
is_only_discrete <- is.null(range_continuous)

if (is_empty) {
expand_limits_continuous_trans(c(0, 1), expand, coord_limits, trans)
} else if (is_only_continuous) {
expand_limits_continuous_trans(range_continuous, expand, coord_limits, trans)
} else if (is_only_discrete) {
expand_limits_continuous_trans(c(1, n_limits), expand, coord_limits, trans)
expand_limits_continuous_trans(c(1, n_discrete_limits), expand, coord_limits, trans)
} else {
# continuous and discrete
limit_info_discrete <- expand_limits_continuous_trans(c(1, n_limits), expand, coord_limits, trans)
limit_info_discrete <- expand_limits_continuous_trans(c(1, n_discrete_limits), expand, coord_limits, trans)

# don't expand continuous range if there is also a discrete range
limit_info_continuous <- expand_limits_continuous_trans(
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,14 @@ test_that("expand_limits_continuous_trans() works with inverted transformations"
expect_identical(limit_info$continuous_range, c(0, 3))
expect_identical(limit_info$continuous_range_coord, c(0, -3))
})

test_that("expand_limits_scale_discrete() begrudgingly handles numeric limits", {
expect_identical(
expand_limits_discrete(
-1:-16,
coord_limits = c(NA, NA),
range_continuous = c(-15, -2)
),
c(-15, -2)
)
})
8 changes: 6 additions & 2 deletions tests/testthat/test-scales-breaks-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,17 @@ test_that("discrete scales with no data have no breaks or labels", {
expect_equal(sc$get_limits(), c(0, 1))
})

test_that("passing continuous limits to a discrete scale generates a warning", {
expect_warning(scale_x_discrete(limits = 1:3), "Continuous limits supplied to discrete scale")
})

test_that("suppressing breaks, minor_breask, and labels works", {
expect_equal(scale_x_continuous(breaks = NULL, limits = c(1, 3))$get_breaks(), NULL)
expect_equal(scale_x_discrete(breaks = NULL, limits = c(1, 3))$get_breaks(), NULL)
expect_equal(scale_x_discrete(breaks = NULL, limits = c("one", "three"))$get_breaks(), NULL)
expect_equal(scale_x_continuous(minor_breaks = NULL, limits = c(1, 3))$get_breaks_minor(), NULL)

expect_equal(scale_x_continuous(labels = NULL, limits = c(1, 3))$get_labels(), NULL)
expect_equal(scale_x_discrete(labels = NULL, limits = c(1, 3))$get_labels(), NULL)
expect_equal(scale_x_discrete(labels = NULL, limits = c("one", "three"))$get_labels(), NULL)

# date, datetime
lims <- as.Date(c("2000/1/1", "2000/2/1"))
Expand Down