Skip to content

Fix after_scale() with plot mappings #4265

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

3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions R/layer.r
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down