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 1 commit
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
Prev Previous commit
Next Next commit
move warning to discrete_scale()
  • Loading branch information
paleolimbot committed Apr 3, 2020
commit d05e2efc5dceab220c45b5097c14bb57503db7d5
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
5 changes: 1 addition & 4 deletions R/scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,9 @@ 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) {
if (is.discrete(limits)) {
n_discrete_limits <- length(limits)
} else {
warn(
"Continuous limits supplied to discrete scale.\nDid you mean `limits = factor(...)` or `scale_*_continuous()`?"
)
n_discrete_limits <- 0
}

Expand Down
17 changes: 7 additions & 10 deletions tests/testthat/test-scale-expansion.r
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ test_that("expand_limits_continuous_trans() works with inverted transformations"
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)
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)
),
"Continuous limits supplied to discrete scale"
c(-15, -2)
)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-scales-breaks-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ test_that("discrete scales with no data have no breaks or labels", {

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