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 3 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
15 changes: 11 additions & 4 deletions R/scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,28 @@ 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) || length(limits) == 0) {
n_discrete_limits <- length(limits)
} else {
warn(
"Continuous limits supplied to discrete scale.\nDid you mean `limits = factor(...)` or `scale_*_continuous()`?"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether this warning is in the wrong place. It's making statements about scale functions and their arguments. Shouldn't it be located inside the appropriate scale functions? (I.e., check the limit argument to scale_*_discrete() rather than checking here.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about that too. scale_x|y_discrete() don't have a limits arg (they pass it via ..., so what do you think about putting it in discrete_scale()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, discrete_scale() seems the right place.

)
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
14 changes: 14 additions & 0 deletions tests/testthat/test-scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ 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() correctly handles numeric limits", {
expect_warning(
expect_identical(
expand_limits_discrete(
-1:-16,
coord_limits = c(NA, NA),
range_continuous = c(-15, -2)
),
c(-15, -2)
),
"Continuous limits supplied to discrete scale"
)
})