diff --git a/R/scale-.r b/R/scale-.r index f8f20f9842..56c036dd29 100644 --- a/R/scale-.r +++ b/R/scale-.r @@ -560,6 +560,17 @@ check_breaks_labels <- function(breaks, labels) { TRUE } +default_transform <- function(self, x) { + new_x <- self$trans$transform(x) + axis <- if ("x" %in% self$aesthetics) "x" else "y" + check_transformation(x, new_x, self$scale_name, axis) + new_x +} + +has_default_transform <- function(scale) { + transform_method <- environment(scale$transform)$f + identical(default_transform, transform_method) || identical(identity, transform_method) +} #' @rdname ggplot2-ggproto #' @format NULL @@ -589,12 +600,7 @@ ScaleContinuous <- ggproto("ScaleContinuous", Scale, !has_data && !has_limits }, - transform = function(self, x) { - new_x <- self$trans$transform(x) - axis <- if ("x" %in% self$aesthetics) "x" else "y" - check_transformation(x, new_x, self$scale_name, axis) - new_x - }, + transform = default_transform, map = function(self, x, limits = self$get_limits()) { x <- self$rescale(self$oob(x, range = limits), limits) @@ -819,9 +825,7 @@ ScaleDiscrete <- ggproto("ScaleDiscrete", Scale, self$range$train(x, drop = self$drop, na.rm = !self$na.translate) }, - transform = function(x) { - x - }, + transform = identity, map = function(self, x, limits = self$get_limits()) { n <- sum(!is.na(limits)) @@ -1001,12 +1005,7 @@ ScaleBinned <- ggproto("ScaleBinned", Scale, self$range$train(x) }, - transform = function(self, x) { - new_x <- self$trans$transform(x) - axis <- if ("x" %in% self$aesthetics) "x" else "y" - check_transformation(x, new_x, self$scale_name, axis) - new_x - }, + transform = default_transform, map = function(self, x, limits = self$get_limits()) { if (self$after.stat) { diff --git a/R/scales-.r b/R/scales-.r index 1537ff0f65..cb1e784670 100644 --- a/R/scales-.r +++ b/R/scales-.r @@ -85,8 +85,8 @@ scales_transform_df <- function(scales, df) { # if the scale contains no trans or the trans is of identity, it doesn't need # to be transformed. idx_skip <- vapply(scales$scales, function(x) { - is.null(x$trans) || - identical(x$trans$transform, identity) + has_default_transform(x) && + (is.null(x$trans) || identical(x$trans$transform, identity)) }, logical(1L)) scale_list <- scales$scales[!idx_skip]