Skip to content

Commit

Permalink
Drop space for unused limits in scale_discrete
Browse files Browse the repository at this point in the history
Fixes #1638 

* Use get_limit in call to dimension
  • Loading branch information
thomasp85 committed Sep 16, 2016
1 parent 048e019 commit 6530a2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Expand Up @@ -87,6 +87,14 @@

* A warning is now issued when a scale transformation introduces infinite
values in a scale (#1696)

* Fixed bug where space for dropped levels in scale_discrete would be preserved
(#1638)

* Fixed bug where scale expansion was not used correctly for discrete scales

* ggplot2 now warns when breaks are dropped due to using continuous data on a
discrete scale (#1589)

* Quantile lines in geom_violin() are no longer affected by the alpha aesthetic
(@mnbram, #1714)
Expand Down
14 changes: 9 additions & 5 deletions R/scale-discrete-.r
Expand Up @@ -73,7 +73,6 @@ scale_y_discrete <- function(..., expand = waiver()) {
#' @usage NULL
#' @export
ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,

train = function(self, x) {
if (is.discrete(x)) {
self$range$train(x, drop = self$drop)
Expand All @@ -84,6 +83,7 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,

get_limits = function(self) {
if (self$is_empty()) return(c(0, 1))

self$limits %||% self$range$range %||% integer()
},

Expand All @@ -106,14 +106,14 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,

dimension = function(self, expand = c(0, 0)) {
c_range <- self$range_c$range
d_range <- self$range$range
d_range <- self$get_limits()

if (self$is_empty()) {
c(0, 1)
} else if (is.null(d_range)) { # only continuous
expand_range(c_range, expand[1], 0 , 1)
} else if (is.null(self$range$range)) { # only continuous
expand_range(c_range, expand[1], expand[2] , 1)
} else if (is.null(c_range)) { # only discrete
expand_range(c(1, length(d_range)), 0, expand[2], 1)
expand_range(c(1, length(d_range)), expand[1], expand[2], 1)
} else { # both
range(
expand_range(c_range, expand[1], 0 , 1),
Expand All @@ -122,6 +122,10 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
}
},

get_breaks = function(self, limits = self$get_limits()) {
ggproto_parent(ScaleDiscrete, self)$get_breaks(limits)
},

clone = function(self) {
new <- ggproto(NULL, self)
new$range <- discrete_range()
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-scale-discrete.R
Expand Up @@ -16,3 +16,10 @@ test_that("discrete ranges also encompas continuous values", {
expect_equal(x_range(base + geom_point(aes(x1)) + geom_point(aes(x2))), c(0, 4))
})

test_that("discrete scale shrinks to range when setting limits", {
df <- data.frame(x = letters[1:10], y = 1:10)
p <- ggplot(df, aes(x, y)) + geom_point() +
scale_x_discrete(limits = c("a", "b"))

expect_equal(layer_scales(p)$x$dimension(c(0, 1)), c(0, 3))
})

0 comments on commit 6530a2b

Please sign in to comment.