Skip to content

Commit 27f97d8

Browse files
committed
Check for occurence of infinite values
Add scale name to warning Clean up warning
1 parent c8086c3 commit 27f97d8

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
* Only one warning is issued when asking for too many levels in
7676
`scale_discrete()` (#1674)
7777

78+
* A warning is now issued when a scale transformation introduces infinite
79+
values in a scale (#1696)
80+
7881
# ggplot2 2.1.0
7982

8083
## New features

R/scale-.r

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,13 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale,
187187
},
188188

189189
transform = function(self, x) {
190-
self$trans$transform(x)
190+
new_x <- self$trans$transform(x)
191+
if (any(is.finite(x) != is.finite(new_x))) {
192+
type <- if (self$scale_name == "position_c") "continuous" else "discrete"
193+
axis <- if ("x" %in% self$aesthetics) "x" else "y"
194+
warning("Transformation introduced infinite values in ", type, " ", axis, "-axis", call. = FALSE)
195+
}
196+
new_x
191197
},
192198

193199
map = function(self, x, limits = self$get_limits()) {

tests/testthat/test-scales.r

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,13 @@ test_that("find_global searches in the right places", {
167167
expect_identical(find_global("scale_colour_hue", emptyenv()),
168168
ggplot2::scale_colour_hue)
169169
})
170+
171+
test_that("Scales warn when transforms introduces non-finite values", {
172+
df <- data.frame(x = c(1e1, 1e5), y = c(0, 100))
173+
174+
p <- ggplot(df, aes(x, y)) +
175+
geom_point(size = 5) +
176+
scale_y_log10()
177+
178+
expect_warning(ggplot_build(p), "Transformation introduced infinite values")
179+
})

0 commit comments

Comments
 (0)