Skip to content

Commit 7960366

Browse files
authored
Forward compatibility: cherry-pick accessors (#6387)
1 parent 4b67b19 commit 7960366

26 files changed

+185
-418
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Config/testthat/edition: 3
7979
Encoding: UTF-8
8080
LazyData: true
8181
Roxygen: list(markdown = TRUE)
82-
RoxygenNote: 7.3.1
82+
RoxygenNote: 7.3.2
8383
Collate:
8484
'ggproto.R'
8585
'ggplot-global.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ S3method(ggplot_add,list)
6767
S3method(ggplot_add,theme)
6868
S3method(ggplot_add,uneval)
6969
S3method(ggplot_build,ggplot)
70+
S3method(ggplot_build,ggplot_built)
7071
S3method(ggplot_gtable,ggplot_built)
7172
S3method(grid.draw,absoluteGrob)
7273
S3method(grid.draw,ggplot)
@@ -420,7 +421,9 @@ export(geom_violin)
420421
export(geom_vline)
421422
export(get_alt_text)
422423
export(get_element_tree)
424+
export(get_geom_defaults)
423425
export(get_guide_data)
426+
export(get_labs)
424427
export(gg_dep)
425428
export(ggplot)
426429
export(ggplotGrob)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# ggplot2 (development version)
2+
3+
* New `get_labs()` function for retrieving completed plot labels
4+
(@teunbrand, #6008).
5+
* New `get_geom_defaults()` for retrieving resolved default aesthetics.
6+
* A new `ggplot_build()` S3 method for <ggplot_built> classes was added, which
7+
returns input unaltered (@teunbrand, #5800).
8+
19
# ggplot2 3.5.1
210

311
This is a small release focusing on fixing regressions from 3.5.0 and

R/geom-defaults.R

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,54 @@ update_stat_defaults <- function(stat, new) {
5353
g$default_aes[names(new)] <- new
5454
invisible()
5555
}
56+
57+
#' Resolve and get geom defaults
58+
#'
59+
#' @param geom Some definition of a geom:
60+
#' * A `function` that creates a layer, e.g. `geom_path()`.
61+
#' * A layer created by such function
62+
#' * A string naming a geom class in snake case without the `geom_`-prefix,
63+
#' e.g. `"contour_filled"`.
64+
#' * A geom class object.
65+
#' @param theme A [`theme`] object. Defaults to the current global theme.
66+
#'
67+
#' @return A list of aesthetics
68+
#' @export
69+
#' @keywords internal
70+
#'
71+
#' @examples
72+
#' # Using a function
73+
#' get_geom_defaults(geom_raster)
74+
#'
75+
#' # Using a layer includes static aesthetics as default
76+
#' get_geom_defaults(geom_tile(fill = "white"))
77+
#'
78+
#' # Using a class name
79+
#' get_geom_defaults("density_2d")
80+
#'
81+
#' # Using a class
82+
#' get_geom_defaults(GeomPoint)
83+
#'
84+
#' # Changed theme
85+
#' get_geom_defaults("point", theme(geom = element_geom(ink = "purple")))
86+
get_geom_defaults <- function(geom, theme = theme_get()) {
87+
theme <- theme %||% list(geom = .default_geom_element)
88+
89+
if (is.function(geom)) {
90+
geom <- geom()
91+
}
92+
if (is.layer(geom)) {
93+
data <- data_frame0(.id = 1L)
94+
data <- geom$compute_geom_2(data = data)
95+
data$.id <- NULL
96+
return(data)
97+
}
98+
if (is.character(geom)) {
99+
geom <- check_subclass(geom, "Geom")
100+
}
101+
if (inherits(geom, "Geom")) {
102+
out <- geom$use_defaults(data = NULL)
103+
return(out)
104+
}
105+
stop_input_type(geom, as_cli("a layer function, string or {.cls Geom} object"))
106+
}

R/guide-colorbar.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ GuideColourbar <- ggproto(
266266
merge = function(self, params, new_guide, new_params) {
267267
new_params$key$.label <- new_params$key$.value <- NULL
268268
params$key <- vec_cbind(params$key, new_params$key)
269+
params$aesthetic <- union(params$aesthetic, new_params$aesthetic)
269270
return(list(guide = self, params = params))
270271
},
271272

R/guide-legend.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ GuideLegend <- ggproto(
204204
cli::cli_warn("Duplicated {.arg override.aes} is ignored.")
205205
}
206206
params$override.aes <- params$override.aes[!duplicated(nms)]
207+
params$aesthetic <- union(params$aesthetic, new_params$aesthetic)
207208

208209
list(guide = self, params = params)
209210
},

R/guides-.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,7 @@ get_guide_data <- function(plot = last_plot(), aesthetic, panel = 1L) {
792792
check_string(aesthetic, allow_empty = FALSE)
793793
aesthetic <- standardise_aes_names(aesthetic)
794794

795-
if (!inherits(plot, "ggplot_built")) {
796-
plot <- ggplot_build(plot)
797-
}
795+
plot <- ggplot_build(plot)
798796

799797
if (!aesthetic %in% c("x", "y", "x.sec", "y.sec", "theta", "r")) {
800798
# Non position guides: check if aesthetic in colnames of key

R/labels.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ ggtitle <- function(label, subtitle = waiver()) {
105105
labs(title = label, subtitle = subtitle)
106106
}
107107

108+
#' @rdname labs
109+
#' @export
110+
#' @param plot A ggplot object
111+
#' @description
112+
#' `get_labs()` retrieves completed labels from a plot.
113+
get_labs <- function(plot = get_last_plot()) {
114+
plot <- ggplot_build(plot)
115+
116+
labs <- plot$plot$labels
117+
118+
xy_labs <- rename(
119+
c(x = plot$layout$resolve_label(plot$layout$panel_scales_x[[1]], labs),
120+
y = plot$layout$resolve_label(plot$layout$panel_scales_y[[1]], labs)),
121+
c(x.primary = "x", x.secondary = "x.sec",
122+
y.primary = "y", y.secondary = "y.sec")
123+
)
124+
125+
labs <- defaults(xy_labs, labs)
126+
127+
guides <- plot$plot$guides
128+
if (length(guides$aesthetics) == 0) {
129+
return(labs)
130+
}
131+
132+
for (aes in guides$aesthetics) {
133+
param <- guides$get_params(aes)
134+
aes <- param$aesthetic # Can have length > 1 when guide was merged
135+
title <- vec_set_names(rep(list(param$title), length(aes)), aes)
136+
labs <- defaults(title, labs)
137+
}
138+
labs
139+
}
140+
108141
#' Extract alt text from a plot
109142
#'
110143
#' This function returns a text that can be used as alt-text in webpages etc.

R/plot-build.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ ggplot_build <- function(plot) {
3030
UseMethod('ggplot_build')
3131
}
3232

33+
#' @export
34+
ggplot_build.ggplot_built <- function(plot) {
35+
# This is a no-op
36+
plot
37+
}
38+
3339
#' @export
3440
ggplot_build.ggplot <- function(plot) {
3541
plot <- plot_clone(plot)

man/get_geom_defaults.Rd

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)