Skip to content

Fix the discrete scale when all values are continuous #3487

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
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
23 changes: 12 additions & 11 deletions R/scale-discrete-.r
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,18 @@ ScaleDiscretePosition <- ggproto("ScaleDiscretePosition", ScaleDiscrete,
},

get_limits = function(self) {
if (self$is_empty()) {
c(0, 1)
} else if (!is.null(self$limits) & !is.function(self$limits)){
self$limits
} else if (is.null(self$limits)) {
self$range$range
} else if (is.function(self$limits)) {
self$limits(self$range$range)
} else {
integer(0)
}
# if scale contains no information, return the default limit
if (self$is_empty()) {
return(c(0, 1))
}

# if self$limits is not NULL and is a function, apply it to range
if (is.function(self$limits)){
return(self$limits(self$range$range))
}

# self$range$range can be NULL because non-discrete values use self$range_c
self$limits %||% self$range$range %||% integer()
},

is_empty = function(self) {
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-scale-discrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ test_that("discrete ranges also encompass continuous values", {
expect_equal(x_range(base + geom_point(aes(x1)) + geom_point(aes(x2))), c(0, 4))
})

test_that("discrete ranges have limits even when all values are continuous", {
scale <- scale_x_discrete()
scale$train(1:3)
expect_identical(scale$get_limits(), integer())
})

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() +
Expand Down