Skip to content
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

Fix scale- and coord-related regressions #3566

Merged
merged 3 commits into from
Nov 14, 2019
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
2 changes: 1 addition & 1 deletion R/guides-axis.r
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ guide_train.axis <- function(guide, scale, aesthetic = NULL) {
}
}

guide$key <- ticks
guide$key <- ticks[is.finite(ticks[[aesthetic]]), ]
}

guide$name <- paste0(guide$name, "_", aesthetic)
Expand Down
27 changes: 24 additions & 3 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,9 @@ Scale <- ggproto("Scale", NULL,
if (is.null(self$limits)) {
self$range$range
} else if (is.function(self$limits)) {
# if limits is a function, it expects to work in data space
self$trans$transform(self$limits(self$trans$inverse(self$range$range)))
self$limits(self$range$range)
} else {
ifelse(is.na(self$limits), self$range$range, self$limits)
self$limits
}
},

Expand Down Expand Up @@ -534,6 +533,12 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
self$range$train(x)
},

is_empty = function(self) {
has_data <- !is.null(self$range$range)
has_limits <- is.function(self$limits) || (!is.null(self$limits) && all(is.finite(self$limits)))
!has_data && !has_limits
},

Comment on lines +536 to +541
Copy link
Member

Choose a reason for hiding this comment

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

Can this be added to the Scale class instead or will it break ScaleDiscrete?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this will cause errors in ScaleDiscrete, where NA is a perfectly valid limit (I think)

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops...I think that adding this implementation of is_empty() to Scale will cause errors in ScaleDiscrete.

transform = function(self, x) {
new_x <- self$trans$transform(x)
axis <- if ("x" %in% self$aesthetics) "x" else "y"
Expand All @@ -555,6 +560,22 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
self$rescaler(x, from = range)
},

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

if (is.null(self$limits)) {
self$range$range
} else if (is.function(self$limits)) {
# if limits is a function, it expects to work in data space
self$trans$transform(self$limits(self$trans$inverse(self$range$range)))
} else {
# NA limits for a continuous scale mean replace with the min/max of data
ifelse(is.na(self$limits), self$range$range, self$limits)
}
},

Comment on lines +563 to +578
Copy link
Member

Choose a reason for hiding this comment

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

Is this just the same as Scale$get_limits()?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's different...Scale$get_limits() doesn't do anything to NA limits, and doesn't apply the function to untransformed limits and then transform the result (because transforms only exist in continuous scales). I'm not sure what was happening with NA limits in discrete scales before.

Copy link
Member

Choose a reason for hiding this comment

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

Can you test if it breaks anything in ScaleDiscrete? I don’t like having such a small method overwriting for no reason

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason is that passing functions as limits for discrete scales errored before (#3448), because Scale$trans is not defined. I've added tests for limit handling for continuous scales, and there are no failing tests otherwise...would adding a similar suite of tests for discrete scales be a reasonable way to resolve this?

dimension = function(self, expand = expansion(0, 0), limits = self$get_limits()) {
expand_limits_scale(self, expand, limits)
},
Expand Down
1 change: 0 additions & 1 deletion R/scale-view.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ view_scale_primary <- function(scale, limits = scale$get_limits(),

if(!scale$is_discrete()) {
breaks <- scale$get_breaks(continuous_range)
breaks <- breaks[is.finite(breaks)]
minor_breaks <- scale$get_breaks_minor(b = breaks, limits = continuous_range)
} else {
breaks <- scale$get_breaks(limits)
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 @@ -81,3 +81,9 @@ test_that("discrete position scales can accept functional limits", {
scale$train(c("a", "b", "c"))
expect_identical(scale$get_limits(), c("c", "b", "a"))
})

test_that("discrete non-position scales can accept functional limits", {
scale <- scale_colour_discrete(limits = rev)
scale$train(c("a", "b", "c"))
expect_identical(scale$get_limits(), c("c", "b", "a"))
})
16 changes: 16 additions & 0 deletions tests/testthat/test-scales-breaks-labels.r
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ test_that("continuous limits accepts functions", {
expect_equal(layer_scales(p)$y$get_limits(), c(range(mpg$hwy)[1] - 10, range(mpg$hwy)[2] + 100))
})

test_that("equal length breaks and labels can be passed to ViewScales with limits", {

test_scale <- scale_x_continuous(
breaks = c(0, 20, 40),
labels = c("0", "20", "40"),
limits = c(10, 30)
)

expect_identical(test_scale$get_breaks(), c(NA, 20, NA))
expect_identical(test_scale$get_labels(), c(c("0", "20", "40")))

test_view_scale <- view_scale_primary(test_scale)
expect_identical(test_view_scale$get_breaks(), c(NA, 20, NA))
expect_identical(test_scale$get_labels(), c(c("0", "20", "40")))
})

# Visual tests ------------------------------------------------------------

test_that("minor breaks draw correctly", {
Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/test-scales.r
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,28 @@ test_that("multiple aesthetics can be set with one function call", {
expect_equal(layer_data(p)$colour, c("cyan", "red", "green"))
expect_equal(layer_data(p)$fill, c("red", "green", "blue"))
})

test_that("limits with NA are replaced with the min/max of the data for continuous scales", {
make_scale <- function(limits = NULL, data = NULL) {
scale <- continuous_scale("aesthetic", scale_name = "test", palette = identity, limits = limits)
if (!is.null(data)) {
scale$train(data)
}
scale
}

# emptiness
expect_true(make_scale()$is_empty())
expect_false(make_scale(limits = c(0, 1))$is_empty())
expect_true(make_scale(limits = c(0, NA))$is_empty())
expect_true(make_scale(limits = c(NA, NA))$is_empty())
expect_true(make_scale(limits = c(NA, 0))$is_empty())

# limits
expect_equal(make_scale(data = 1:5)$get_limits(), c(1, 5))
expect_equal(make_scale(limits = c(1, 5))$get_limits(), c(1, 5))
expect_equal(make_scale(limits = c(NA, NA))$get_limits(), c(0, 1))
expect_equal(make_scale(limits = c(NA, NA), data = 1:5)$get_limits(), c(1, 5))
expect_equal(make_scale(limits = c(1, NA), data = 1:5)$get_limits(), c(1, 5))
expect_equal(make_scale(limits = c(NA, 5), data = 1:5)$get_limits(), c(1, 5))
})