Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
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)
Copy link
Member

Choose a reason for hiding this comment

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

And mention the expand change?


* 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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))
})