Skip to content

Commit 4c1964b

Browse files
committed
Restore notion of plot environment to find default scales
1 parent 84a3544 commit 4c1964b

File tree

6 files changed

+14
-21
lines changed

6 files changed

+14
-21
lines changed

R/layer.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Layer <- ggproto("Layer", NULL,
227227
data <- data[res, , drop = FALSE]
228228
}
229229

230-
scales_add_defaults(plot$scales, data, aesthetics)
230+
scales_add_defaults(plot$scales, data, aesthetics, plot$plot_env)
231231

232232
# Evaluate and check aesthetics
233233
aesthetics <- compact(aesthetics)
@@ -287,7 +287,7 @@ Layer <- ggproto("Layer", NULL,
287287
names(stat_data) <- names(new)
288288

289289
# Add any new scales, if needed
290-
scales_add_defaults(plot$scales, data, new)
290+
scales_add_defaults(plot$scales, data, new, plot$plot_env)
291291
# Transform the values, if the scale say it's ok
292292
# (see stat_spoke for one exception)
293293
if (self$stat$retransform) {

R/plot.r

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,23 @@
7070
#' width = 0.4
7171
#' )
7272
ggplot <- function(data = NULL, mapping = aes(), ...,
73-
environment = NULL) {
73+
environment = parent.frame()) {
7474
UseMethod("ggplot")
7575
}
7676

7777
#' @export
7878
ggplot.default <- function(data = NULL, mapping = aes(), ...,
79-
environment = NULL) {
79+
environment = parent.frame()) {
8080
ggplot.data.frame(fortify(data, ...), mapping, environment = environment)
8181
}
8282

8383
#' @export
8484
ggplot.data.frame <- function(data, mapping = aes(), ...,
85-
environment = NULL) {
85+
environment = parent.frame()) {
8686
if (!missing(mapping) && !inherits(mapping, "uneval")) {
8787
stop("Mapping should be created with `aes() or `aes_()`.", call. = FALSE)
8888
}
8989

90-
if (!is.null(environment)) {
91-
stop(
92-
"`environment` is deprecated: environments are now captured by `aes()`",
93-
call. = FALSE
94-
)
95-
}
96-
9790
p <- structure(list(
9891
data = data,
9992
layers = list(),
@@ -102,7 +95,7 @@ ggplot.data.frame <- function(data, mapping = aes(), ...,
10295
theme = list(),
10396
coordinates = coord_cartesian(default = TRUE),
10497
facet = facet_null(),
105-
plot_env = parent.frame()
98+
plot_env = environment
10699
), class = c("gg", "ggplot"))
107100

108101
p$labels <- make_labels(mapping)
@@ -113,7 +106,7 @@ ggplot.data.frame <- function(data, mapping = aes(), ...,
113106

114107
#' @export
115108
ggplot.grouped_df <- function(data, mapping = aes(), ...,
116-
environment = NULL) {
109+
environment = parent.frame()) {
117110

118111
data$.group <- dplyr::group_indices(data)
119112
mapping$group <- mapping$group %||% quote(.group)

R/quick-plot.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
123123
}
124124
}
125125

126-
p <- ggplot(data, mapping, environment = NULL)
126+
p <- ggplot(data, mapping, environment = caller_env)
127127

128128
if (is.null(facets)) {
129129
p <- p + facet_null()
@@ -139,7 +139,7 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins = FALSE,
139139
for (g in geom) {
140140
# We reevaluate constants once per geom for historical reasons?
141141
params <- lapply(consts, rlang::eval_tidy)
142-
p <- p + do.call(paste0("geom_", g), params)
142+
p <- p + do.call(paste0("geom_", g), params, envir = caller_env)
143143
}
144144

145145
logv <- function(var) var %in% strsplit(log, "")[[1]]

R/scale-type.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
find_scale <- function(aes, x) {
1+
find_scale <- function(aes, x, env) {
22
type <- scale_type(x)
33
candidates <- paste("scale", aes, type, sep = "_")
44

55
for (scale in candidates) {
6-
scale_f <- find_global(scale, globalenv(), mode = "function")
6+
scale_f <- find_global(scale, env, mode = "function")
77
if (!is.null(scale_f))
88
return(scale_f())
99
}

R/scales-.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ scales_transform_df <- function(scales, df) {
8888

8989
# @param aesthetics A list of aesthetic-variable mappings. The name of each
9090
# item is the aesthetic, and the value of each item is the variable in data.
91-
scales_add_defaults <- function(scales, data, aesthetics) {
91+
scales_add_defaults <- function(scales, data, aesthetics, env) {
9292
if (is.null(aesthetics)) return()
9393
names(aesthetics) <- unlist(lapply(names(aesthetics), aes_to_scale))
9494

@@ -99,7 +99,7 @@ scales_add_defaults <- function(scales, data, aesthetics) {
9999
datacols <- lapply(aesthetics[new_aesthetics], rlang::eval_tidy, data = data)
100100

101101
for (aes in names(datacols)) {
102-
scales$add(find_scale(aes, datacols[[aes]]))
102+
scales$add(find_scale(aes, datacols[[aes]], env))
103103
}
104104

105105
}

man/ggplot.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)