diff --git a/NEWS.md b/NEWS.md index effc4b5d96..b25ea77384 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,9 @@ * Improved error with hint when piping a `ggplot` object into a facet function (#4379, @mitchelloharawild). +* Fix a bug that `after_stat()` and `after_scale()` cannot refer to aesthetics + if it's specified in the plot-global mapping (@yutannihilation, #4260). + # ggplot2 3.3.3 This is a small patch release mainly intended to address changes in R and CRAN. It further changes the licensing model of ggplot2 to an MIT license. diff --git a/R/layer.r b/R/layer.r index 0db859978a..44c75fc0df 100644 --- a/R/layer.r +++ b/R/layer.r @@ -203,16 +203,18 @@ Layer <- ggproto("Layer", NULL, # hook to allow a layer access to the final layer data # in input form and to global plot info setup_layer = function(self, data, plot) { + # For annotation geoms, it is useful to be able to ignore the default aes + if (isTRUE(self$inherit.aes)) { + self$mapping <- defaults(self$mapping, plot$mapping) + # defaults() strips class, but it needs to be preserved for now + class(self$mapping) <- "uneval" + } + data }, compute_aesthetics = function(self, data, plot) { - # For annotation geoms, it is useful to be able to ignore the default aes - if (self$inherit.aes) { - aesthetics <- defaults(self$mapping, plot$mapping) - } else { - aesthetics <- self$mapping - } + aesthetics <- self$mapping # Drop aesthetics that are set or calculated set <- names(aesthetics) %in% names(self$aes_params) @@ -289,9 +291,6 @@ Layer <- ggproto("Layer", NULL, # Assemble aesthetics from layer, plot and stat mappings aesthetics <- self$mapping - if (self$inherit.aes) { - aesthetics <- defaults(aesthetics, plot$mapping) - } aesthetics <- defaults(aesthetics, self$stat$default_aes) aesthetics <- compact(aesthetics)