Skip to content

Commit 885c3c1

Browse files
authored
Fix bug in discrete position scales with continuous limits (fixes #3918) (#3919)
* fix manual limits with discrete scale that was only trained using continuous values * add test for the case of no discrete limits * add warning if user supplies continuous limits to a discrete scale * move warning to discrete_scale() * add test for continuous limits
1 parent efd7f0d commit 885c3c1

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

R/scale-.r

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
164164

165165
check_breaks_labels(breaks, labels)
166166

167+
if (!is.function(limits) && (length(limits) > 0) && !is.discrete(limits)) {
168+
warn(
169+
glue(
170+
"
171+
Continuous limits supplied to discrete scale.
172+
Did you mean `limits = factor(...)` or `scale_*_continuous()`?"
173+
)
174+
)
175+
}
176+
167177
position <- match.arg(position, c("left", "right", "top", "bottom"))
168178

169179
# If the scale is non-positional, break = NULL means removing the guide

R/scale-expansion.r

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,25 @@ expand_limits_continuous_trans <- function(limits, expand = expansion(0, 0),
200200
expand_limits_discrete_trans <- function(limits, expand = expansion(0, 0),
201201
coord_limits = c(NA, NA), trans = identity_trans(),
202202
range_continuous = NULL) {
203+
if (is.discrete(limits)) {
204+
n_discrete_limits <- length(limits)
205+
} else {
206+
n_discrete_limits <- 0
207+
}
203208

204-
n_limits <- length(limits)
205209
is_empty <- is.null(limits) && is.null(range_continuous)
206-
is_only_continuous <- n_limits == 0
210+
is_only_continuous <- n_discrete_limits == 0
207211
is_only_discrete <- is.null(range_continuous)
208212

209213
if (is_empty) {
210214
expand_limits_continuous_trans(c(0, 1), expand, coord_limits, trans)
211215
} else if (is_only_continuous) {
212216
expand_limits_continuous_trans(range_continuous, expand, coord_limits, trans)
213217
} else if (is_only_discrete) {
214-
expand_limits_continuous_trans(c(1, n_limits), expand, coord_limits, trans)
218+
expand_limits_continuous_trans(c(1, n_discrete_limits), expand, coord_limits, trans)
215219
} else {
216220
# continuous and discrete
217-
limit_info_discrete <- expand_limits_continuous_trans(c(1, n_limits), expand, coord_limits, trans)
221+
limit_info_discrete <- expand_limits_continuous_trans(c(1, n_discrete_limits), expand, coord_limits, trans)
218222

219223
# don't expand continuous range if there is also a discrete range
220224
limit_info_continuous <- expand_limits_continuous_trans(

tests/testthat/test-scale-expansion.r

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,14 @@ test_that("expand_limits_continuous_trans() works with inverted transformations"
103103
expect_identical(limit_info$continuous_range, c(0, 3))
104104
expect_identical(limit_info$continuous_range_coord, c(0, -3))
105105
})
106+
107+
test_that("expand_limits_scale_discrete() begrudgingly handles numeric limits", {
108+
expect_identical(
109+
expand_limits_discrete(
110+
-1:-16,
111+
coord_limits = c(NA, NA),
112+
range_continuous = c(-15, -2)
113+
),
114+
c(-15, -2)
115+
)
116+
})

tests/testthat/test-scales-breaks-labels.r

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,17 @@ test_that("discrete scales with no data have no breaks or labels", {
138138
expect_equal(sc$get_limits(), c(0, 1))
139139
})
140140

141+
test_that("passing continuous limits to a discrete scale generates a warning", {
142+
expect_warning(scale_x_discrete(limits = 1:3), "Continuous limits supplied to discrete scale")
143+
})
144+
141145
test_that("suppressing breaks, minor_breask, and labels works", {
142146
expect_equal(scale_x_continuous(breaks = NULL, limits = c(1, 3))$get_breaks(), NULL)
143-
expect_equal(scale_x_discrete(breaks = NULL, limits = c(1, 3))$get_breaks(), NULL)
147+
expect_equal(scale_x_discrete(breaks = NULL, limits = c("one", "three"))$get_breaks(), NULL)
144148
expect_equal(scale_x_continuous(minor_breaks = NULL, limits = c(1, 3))$get_breaks_minor(), NULL)
145149

146150
expect_equal(scale_x_continuous(labels = NULL, limits = c(1, 3))$get_labels(), NULL)
147-
expect_equal(scale_x_discrete(labels = NULL, limits = c(1, 3))$get_labels(), NULL)
151+
expect_equal(scale_x_discrete(labels = NULL, limits = c("one", "three"))$get_labels(), NULL)
148152

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

0 commit comments

Comments
 (0)