Skip to content

Commit c37317b

Browse files
committed
Use @ as accessor
1 parent 032ca6a commit c37317b

27 files changed

+173
-168
lines changed

R/facet-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ get_strip_labels <- function(plot = get_last_plot()) {
401401
plot <- ggplot_build(plot)
402402
layout <- plot$layout$layout
403403
params <- plot$layout$facet_params
404-
plot$plot$facet$format_strip_labels(layout, params)
404+
plot$plot@facet$format_strip_labels(layout, params)
405405
}
406406

407407
# A "special" value, currently not used but could be used to determine

R/guides-.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ get_guide_data <- function(plot = get_last_plot(), aesthetic, panel = 1L) {
832832

833833
if (!aesthetic %in% c("x", "y", "x.sec", "y.sec", "theta", "r")) {
834834
# Non position guides: check if aesthetic in colnames of key
835-
keys <- lapply(plot$plot$guides$params, `[[`, "key")
835+
keys <- lapply(plot$plot@guides$params, `[[`, "key")
836836
keep <- vapply(keys, function(x) any(colnames(x) %in% aesthetic), logical(1))
837837
keys <- switch(sum(keep) + 1, NULL, keys[[which(keep)]], keys[keep])
838838
return(keys)

R/labels.R

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' update_labels(p, list(colour = "Fail silently"))
1313
update_labels <- function(p, labels) {
1414
p <- plot_clone(p)
15-
p$labels <- defaults(labels, p$labels)
15+
p@labels <- labs(!!!defaults(labels, p@labels))
1616
p
1717
}
1818

@@ -69,7 +69,7 @@ setup_plot_labels <- function(plot, layers, data) {
6969
# Warn for spurious labels that don't have a mapping.
7070
# Note: sometimes, 'x' and 'y' might not have a mapping, like in
7171
# `geom_function()`. We can display these labels anyway, so we include them.
72-
plot_labels <- plot$labels
72+
plot_labels <- plot@labels
7373
known_labels <- c(names(labels), fn_fmls_names(labs), "x", "y")
7474
extra_labels <- setdiff(names(plot_labels), known_labels)
7575

@@ -102,7 +102,7 @@ setup_plot_labels <- function(plot, layers, data) {
102102
})
103103
}
104104

105-
defaults(plot_labels, labels)
105+
labs(!!!defaults(plot_labels, labels))
106106
}
107107

108108
#' Modify axis, legend, and plot labels
@@ -220,7 +220,7 @@ ggtitle <- function(label, subtitle = waiver()) {
220220
get_labs <- function(plot = get_last_plot()) {
221221
plot <- ggplot_build(plot)
222222

223-
labs <- plot$plot$labels
223+
labs <- plot$plot@labels
224224

225225
xy_labs <- rename(
226226
c(x = plot$layout$resolve_label(plot$layout$panel_scales_x[[1]], labs),
@@ -231,7 +231,7 @@ get_labs <- function(plot = get_last_plot()) {
231231

232232
labs <- defaults(xy_labs, labs)
233233

234-
guides <- plot$plot$guides
234+
guides <- plot$plot@guides
235235
if (length(guides$aesthetics) == 0) {
236236
return(labs)
237237
}
@@ -281,19 +281,19 @@ get_alt_text <- function(p, ...) {
281281
}
282282
#' @export
283283
get_alt_text.ggplot <- function(p, ...) {
284-
alt <- p$labels[["alt"]] %||% ""
284+
alt <- p@labels[["alt"]] %||% ""
285285
if (!is.function(alt)) {
286286
return(alt)
287287
}
288-
p$labels[["alt"]] <- NULL
288+
p@labels[["alt"]] <- NULL
289289
build <- ggplot_build(p)
290-
build$plot$labels[["alt"]] <- alt
290+
build$plot@labels[["alt"]] <- alt
291291
get_alt_text(build)
292292
}
293293
#' @export
294294
get_alt_text.ggplot_built <- function(p, ...) {
295-
alt <- p$plot$labels[["alt"]] %||% ""
296-
p$plot$labels[["alt"]] <- NULL
295+
alt <- p$plot@labels[["alt"]] %||% ""
296+
p$plot@labels[["alt"]] <- NULL
297297
if (is.function(alt)) alt(p$plot) else alt
298298
}
299299
#' @export
@@ -347,8 +347,8 @@ get_alt_text.gtable <- function(p, ...) {
347347
#'
348348
generate_alt_text <- function(p) {
349349
# Combine titles
350-
if (!is.null(p$label$title %||% p$labels$subtitle)) {
351-
title <- sub("\\.?$", "", c(p$labels$title, p$labels$subtitle))
350+
if (!is.null(p@labels$title %||% p@labels$subtitle)) {
351+
title <- sub("\\.?$", "", c(p@labels$title, p@labels$subtitle))
352352
if (length(title) == 2) {
353353
title <- paste0(title[1], ": ", title[2])
354354
}
@@ -364,7 +364,7 @@ generate_alt_text <- function(p) {
364364
axes <- safe_string(axes)
365365

366366
# Get layer types
367-
layers <- vapply(p$layers, function(l) snake_class(l$geom), character(1))
367+
layers <- vapply(p@layers, function(l) snake_class(l$geom), character(1))
368368
layers <- sub("_", " ", sub("^geom_", "", unique0(layers)))
369369
if (length(layers) == 1) {
370370
layers <- paste0(" using a ", layers, " layer")
@@ -375,21 +375,21 @@ generate_alt_text <- function(p) {
375375

376376
# Combine
377377
alt <- paste0(title, "A plot", axes, layers, ".")
378-
if (!is.null(p$labels$alt_insight)) {
379-
alt <- paste0(alt, " ", p$labels$alt_insight)
378+
if (!is.null(p@labels$alt_insight)) {
379+
alt <- paste0(alt, " ", p@labels$alt_insight)
380380
}
381381
as.character(alt)
382382
}
383383
safe_string <- function(string) {
384384
if (length(string) == 0) "" else string
385385
}
386386
scale_description <- function(p, name) {
387-
scale <- p$scales$get_scales(name)
387+
scale <- p@scales$get_scales(name)
388388
if (is.null(scale)) {
389-
lab <- p$labels[[name]]
389+
lab <- p@labels[[name]]
390390
type <- "the"
391391
} else {
392-
lab <- scale$make_title(scale$name %|W|% p$labels[[name]])
392+
lab <- scale$make_title(scale$name %|W|% p@labels[[name]])
393393
type <- "a continuous"
394394
if (scale$is_discrete()) type <- "a discrete"
395395
if (inherits(scale, "ScaleBinned")) type <- "a binned"

R/layer.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ Layer <- ggproto("Layer", NULL,
265265
setup_layer = function(self, data, plot) {
266266
# For annotation geoms, it is useful to be able to ignore the default aes
267267
if (isTRUE(self$inherit.aes)) {
268-
self$computed_mapping <- mapping(defaults(self$mapping, plot$mapping))
268+
self$computed_mapping <- mapping(defaults(self$mapping, plot@mapping))
269269

270270
# Inherit size as linewidth from global mapping
271271
if (self$geom$rename_size &&
272-
"size" %in% names(plot$mapping) &&
272+
"size" %in% names(plot@mapping) &&
273273
!"linewidth" %in% names(self$computed_mapping) &&
274274
"linewidth" %in% self$geom$aesthetics()) {
275-
self$computed_mapping$size <- plot$mapping$size
275+
self$computed_mapping$size <- plot@mapping$size
276276
deprecate_soft0("3.4.0", I("Using `size` aesthetic for lines"), I("`linewidth`"))
277277
}
278278
} else {
@@ -300,7 +300,7 @@ Layer <- ggproto("Layer", NULL,
300300

301301
# Evaluate aesthetics
302302
evaled <- eval_aesthetics(aesthetics, data)
303-
plot$scales$add_defaults(evaled, plot$plot_env)
303+
plot@scales$add_defaults(evaled, plot@plot_env)
304304

305305
# Check for discouraged usage in mapping
306306
warn_for_aes_extract_usage(aesthetics, data[setdiff(names(data), "PANEL")])
@@ -370,7 +370,7 @@ Layer <- ggproto("Layer", NULL,
370370
if (length(new) == 0) return(data)
371371

372372
# data needs to be non-scaled
373-
data_orig <- plot$scales$backtransform_df(data)
373+
data_orig <- plot@scales$backtransform_df(data)
374374

375375
# Add map stat output to aesthetics
376376
stat_data <- eval_aesthetics(
@@ -387,11 +387,11 @@ Layer <- ggproto("Layer", NULL,
387387
stat_data <- data_frame0(!!!stat_data)
388388

389389
# Add any new scales, if needed
390-
plot$scales$add_defaults(stat_data, plot$plot_env)
390+
plot@scales$add_defaults(stat_data, plot@plot_env)
391391
# Transform the values, if the scale say it's ok
392392
# (see stat_spoke for one exception)
393393
if (self$stat$retransform) {
394-
stat_data <- plot$scales$transform_df(stat_data)
394+
stat_data <- plot@scales$transform_df(stat_data)
395395
}
396396
stat_data <- cleanup_mismatched_data(stat_data, nrow(data), "after_stat")
397397

R/plot-build.R

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,28 @@ ggplot_build.ggplot_built <- function(plot) {
3939
#' @export
4040
ggplot_build.ggplot <- function(plot) {
4141
plot <- plot_clone(plot)
42-
if (length(plot$layers) == 0) {
42+
if (length(plot@layers) == 0) {
4343
plot <- plot + geom_blank()
4444
}
4545

46-
layers <- plot$layers
46+
layers <- plot@layers
4747
data <- rep(list(NULL), length(layers))
4848

49-
scales <- plot$scales
49+
scales <- plot@scales
5050

5151
# Allow all layers to make any final adjustments based
5252
# on raw input data and plot info
53-
data <- by_layer(function(l, d) l$layer_data(plot$data), layers, data, "computing layer data")
53+
data <- by_layer(function(l, d) l$layer_data(plot@data), layers, data, "computing layer data")
5454
data <- by_layer(function(l, d) l$setup_layer(d, plot), layers, data, "setting up layer")
5555

5656
# Initialise panels, add extra data for margins & missing faceting
5757
# variables, and add on a PANEL variable to data
58-
layout <- create_layout(plot$facet, plot$coordinates, plot$layout)
59-
data <- layout$setup(data, plot$data, plot$plot_env)
58+
layout <- create_layout(plot@facet, plot@coordinates, plot@layout)
59+
data <- layout$setup(data, plot@data, plot@plot_env)
6060

6161
# Compute aesthetics to produce data with generalised variable names
6262
data <- by_layer(function(l, d) l$compute_aesthetics(d, plot), layers, data, "computing aesthetics")
63-
plot$labels <- setup_plot_labels(plot, layers, data)
63+
plot@labels <- setup_plot_labels(plot, layers, data)
6464
data <- .ignore_data(data)
6565

6666
# Transform all scales
@@ -80,7 +80,7 @@ ggplot_build.ggplot <- function(plot) {
8080
data <- by_layer(function(l, d) l$map_statistic(d, plot), layers, data, "mapping stat to aesthetics")
8181

8282
# Make sure missing (but required) aesthetics are added
83-
plot$scales$add_missing(c("x", "y"), plot$plot_env)
83+
plot@scales$add_missing(c("x", "y"), plot@plot_env)
8484

8585
# Reparameterise geoms from (e.g.) y and width to ymin and ymax
8686
data <- by_layer(function(l, d) l$compute_geom_1(d), layers, data, "setting up geom")
@@ -98,27 +98,27 @@ ggplot_build.ggplot <- function(plot) {
9898
data <- layout$map_position(data)
9999

100100
# Hand off position guides to layout
101-
layout$setup_panel_guides(plot$guides, plot$layers)
101+
layout$setup_panel_guides(plot@guides, plot@layers)
102102

103103
# Complete the plot's theme
104-
plot$theme <- plot_theme(plot)
104+
plot@theme <- plot_theme(plot)
105105

106106
# Train and map non-position scales and guides
107107
npscales <- scales$non_position_scales()
108108
if (npscales$n() > 0) {
109-
npscales$set_palettes(plot$theme)
109+
npscales$set_palettes(plot@theme)
110110
lapply(data, npscales$train_df)
111-
plot$guides <- plot$guides$build(npscales, plot$layers, plot$labels, data, plot$theme)
111+
plot@guides <- plot@guides$build(npscales, plot@layers, plot@labels, data, plot@theme)
112112
data <- lapply(data, npscales$map_df)
113113
} else {
114114
# Only keep custom guides if there are no non-position scales
115-
plot$guides <- plot$guides$get_custom()
115+
plot@guides <- plot@guides$get_custom()
116116
}
117117
data <- .expose_data(data)
118118

119119
# Fill in defaults etc.
120120
data <- by_layer(
121-
function(l, d) l$compute_geom_2(d, theme = plot$theme),
121+
function(l, d) l$compute_geom_2(d, theme = plot@theme),
122122
layers, data, "setting up geom aesthetics"
123123
)
124124

@@ -129,7 +129,7 @@ ggplot_build.ggplot <- function(plot) {
129129
data <- layout$finish_data(data)
130130

131131
# Consolidate alt-text
132-
plot$labels$alt <- get_alt_text(plot)
132+
plot@labels$alt <- get_alt_text(plot)
133133

134134
structure(
135135
list(data = data, layout = layout, plot = plot),
@@ -169,7 +169,7 @@ layer_scales <- get_panel_scales
169169
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
170170
b <- ggplot_build(plot)
171171

172-
b$plot$layers[[i]]$draw_geom(b$data[[i]], b$layout)
172+
b$plot@layers[[i]]$draw_geom(b$data[[i]], b$layout)
173173
}
174174

175175
#' @export
@@ -196,7 +196,7 @@ layer_grob <- get_layer_grob
196196
#' @export
197197
ggplot_gtable <- function(data) {
198198
# Attaching the plot env to be fetched by deprecations etc.
199-
attach_plot_env(data$plot$plot_env)
199+
attach_plot_env(data$plot@plot_env)
200200

201201
UseMethod('ggplot_gtable')
202202
}
@@ -206,33 +206,33 @@ ggplot_gtable.ggplot_built <- function(data) {
206206
plot <- data$plot
207207
layout <- data$layout
208208
data <- data$data
209-
theme <- plot$theme
209+
theme <- plot@theme
210210

211-
geom_grobs <- by_layer(function(l, d) l$draw_geom(d, layout), plot$layers, data, "converting geom to grob")
211+
geom_grobs <- by_layer(function(l, d) l$draw_geom(d, layout), plot@layers, data, "converting geom to grob")
212212

213-
plot_table <- layout$render(geom_grobs, data, theme, plot$labels)
213+
plot_table <- layout$render(geom_grobs, data, theme, plot@labels)
214214

215215
# Legends
216-
legend_box <- plot$guides$assemble(theme)
216+
legend_box <- plot@guides$assemble(theme)
217217
plot_table <- table_add_legends(plot_table, legend_box, theme)
218218

219219
# Title
220220
title <- element_render(
221-
theme, "plot.title", plot$labels$title,
221+
theme, "plot.title", plot@labels$title,
222222
margin_y = TRUE, margin_x = TRUE
223223
)
224224
title_height <- grobHeight(title)
225225

226226
# Subtitle
227227
subtitle <- element_render(
228-
theme, "plot.subtitle", plot$labels$subtitle,
228+
theme, "plot.subtitle", plot@labels$subtitle,
229229
margin_y = TRUE, margin_x = TRUE
230230
)
231231
subtitle_height <- grobHeight(subtitle)
232232

233233
# whole plot annotation
234234
caption <- element_render(
235-
theme, "plot.caption", plot$labels$caption,
235+
theme, "plot.caption", plot@labels$caption,
236236
margin_y = TRUE, margin_x = TRUE
237237
)
238238
caption_height <- grobHeight(caption)
@@ -283,7 +283,7 @@ ggplot_gtable.ggplot_built <- function(data) {
283283
plot_table <- gtable_add_grob(plot_table, caption, name = "caption",
284284
t = -1, b = -1, l = caption_l, r = caption_r, clip = "off")
285285

286-
plot_table <- table_add_tag(plot_table, plot$labels$tag, theme)
286+
plot_table <- table_add_tag(plot_table, plot@labels$tag, theme)
287287

288288
# Margins
289289
plot_margin <- calc_element("plot.margin", theme) %||% margin()
@@ -298,7 +298,7 @@ ggplot_gtable.ggplot_built <- function(data) {
298298
}
299299

300300
# add alt-text as attribute
301-
attr(plot_table, "alt-label") <- plot$labels$alt
301+
attr(plot_table, "alt-label") <- plot@labels$alt
302302

303303
plot_table
304304
}

0 commit comments

Comments
 (0)