Skip to content

Check for occurence of infinite values #1703

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
merged 1 commit into from
Aug 25, 2016
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
* Only one warning is issued when asking for too many levels in
`scale_discrete()` (#1674)

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

# ggplot2 2.1.0

## New features
Expand Down
8 changes: 7 additions & 1 deletion R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
},

transform = function(self, x) {
self$trans$transform(x)
new_x <- self$trans$transform(x)
if (any(is.finite(x) != is.finite(new_x))) {
type <- if (self$scale_name == "position_c") "continuous" else "discrete"
axis <- if ("x" %in% self$aesthetics) "x" else "y"
warning("Transformation introduced infinite values in ", type, " ", axis, "-axis", call. = FALSE)
}
new_x
},

map = function(self, x, limits = self$get_limits()) {
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-scales.r
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,13 @@ test_that("find_global searches in the right places", {
expect_identical(find_global("scale_colour_hue", emptyenv()),
ggplot2::scale_colour_hue)
})

test_that("Scales warn when transforms introduces non-finite values", {
df <- data.frame(x = c(1e1, 1e5), y = c(0, 100))

p <- ggplot(df, aes(x, y)) +
geom_point(size = 5) +
scale_y_log10()

expect_warning(ggplot_build(p), "Transformation introduced infinite values")
})