Skip to content

Commit 89893c7

Browse files
committed
Better limits + dimensions defaults for scale_discrete.
Fixes tidyverse#1542
1 parent fb3931c commit 89893c7

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373

7474
* The default scale for columns of class "AsIs" is now "identity" (#1518).
7575

76+
* `scale_*_discrete()` has better defaults when used with purely continuous
77+
data (#1542).
78+
7679
* `scale_size()` warns when used with categorical data.
7780

7881
* `scale_size()`, `scale_colour()`, and `scale_fill()` gain date and date-time

R/scale-discrete-.r

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,9 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
9292
}
9393
},
9494

95-
# If range not available from discrete range, implies discrete scale been
96-
# used with purely continuous data, so construct limits accordingly
9795
get_limits = function(self) {
9896
if (self$is_empty()) return(c(0, 1))
99-
100-
dis_limits <- function(x) seq.int(floor(min(x)), ceiling(max(x)), by = 1L)
101-
102-
self$limits %||% self$range$range %||% dis_limits(self$range_c$range)
97+
self$limits %||% self$range$range %||% integer()
10398
},
10499

105100
is_empty = function(self) {
@@ -120,16 +115,21 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
120115
},
121116

122117
dimension = function(self, expand = c(0, 0)) {
123-
disc_range <- c(1, length(self$get_limits()))
124-
disc <- expand_range(disc_range, 0, expand[2], 1)
125-
126-
# if no data was trained (i.e. range_c is infinite) return disc range
127-
if (any(is.infinite(self$range_c$range))) {
128-
return(disc)
118+
c_range <- self$range_c$range
119+
d_range <- self$range$range
120+
121+
if (self$is_empty()) {
122+
c(0, 1)
123+
} else if (is.null(d_range)) { # only continuous
124+
expand_range(c_range, expand[1], 0 , 1)
125+
} else if (is.null(c_range)) { # only discrete
126+
expand_range(c(1, length(d_range)), 0, expand[2], 1)
127+
} else { # both
128+
range(
129+
expand_range(c_range, expand[1], 0 , 1),
130+
expand_range(c(1, length(d_range)), 0, expand[2], 1)
131+
)
129132
}
130-
131-
cont <- expand_range(self$range_c$range, expand[1], 0, expand[2])
132-
range(disc, cont)
133133
},
134134

135135
clone = function(self) {

tests/testthat/test-scale-discrete.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
context("scale_discrete")
2+
3+
# Ranges ------------------------------------------------------------------
4+
5+
test_that("discrete ranges also encompas continuous values", {
6+
df <- data.frame(x1 = c("a", "b", "c"), x2 = c(0, 2, 4), y = 1:3)
7+
8+
base <- ggplot(df, aes(y = y)) + scale_x_discrete()
9+
10+
x_range <- function(x) {
11+
layer_scales(x)$x$dimension()
12+
}
13+
14+
expect_equal(x_range(base + geom_point(aes(x1))), c(1, 3))
15+
expect_equal(x_range(base + geom_point(aes(x2))), c(0, 4))
16+
expect_equal(x_range(base + geom_point(aes(x1)) + geom_point(aes(x2))), c(0, 4))
17+
})
18+

0 commit comments

Comments
 (0)