Skip to content

Commit f51324c

Browse files
committed
backward compatibility for ggplot_add()
1 parent 32fa918 commit f51324c

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ S3method(fortify,summary.glht)
5656
S3method(fortify,tbl)
5757
S3method(fortify,tbl_df)
5858
S3method(ggplot_add,default)
59+
S3method(ggplot_build,default)
5960
S3method(grid.draw,absoluteGrob)
6061
S3method(grobHeight,absoluteGrob)
6162
S3method(grobHeight,zeroGrob)
@@ -286,6 +287,7 @@ export(autoplot)
286287
export(benchplot)
287288
export(binned_scale)
288289
export(borders)
290+
export(build_ggplot)
289291
export(calc_element)
290292
export(check_device)
291293
export(class_ggplot)

R/plot-build.R

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#' Build ggplot for rendering.
22
#'
3-
#' `ggplot_build()` takes the plot object, and performs all steps necessary
3+
#' `build_ggplot()` takes the plot object, and performs all steps necessary
44
#' to produce an object that can be rendered. This function outputs two pieces:
55
#' a list of data frames (one for each layer), and a panel object, which
6-
#' contain all information about axis limits, breaks etc.
6+
#' contain all information about axis limits, breaks etc. The `ggplot_build()`
7+
#' function is vestigial and `build_ggplot()` should be used instead.
78
#'
89
#' `get_layer_data()`, `get_layer_grob()`, and `get_panel_scales()` are helper
910
#' functions that return the data, grob, or scales associated with a given
@@ -15,6 +16,7 @@
1516
#' plot). In `get_panel_scales()`, the row of a facet to return scales for.
1617
#' @param j An integer. In `get_panel_scales()`, the column of a facet to return
1718
#' scales for.
19+
#' @param ... Not currently in use.
1820
#' @seealso
1921
#' [print.ggplot()] and [benchplot()] for
2022
#' functions that contain the complete set of steps for generating
@@ -23,19 +25,19 @@
2325
#' The `r link_book("build step section", "internals#sec-ggplotbuild")`
2426
#' @keywords internal
2527
#' @export
26-
ggplot_build <- S7::new_generic("ggplot_build", "plot", fun = function(plot) {
27-
# Attaching the plot env to be fetched by deprecations etc.
28-
if (S7::S7_inherits(plot) && S7::prop_exists(plot, "plot_env")) {
29-
attach_plot_env(plot@plot_env)
28+
build_ggplot <- S7::new_generic("build_ggplot", "plot", fun = function(plot, ...) {
29+
env <- try_prop(plot, "plot_env")
30+
if (!is.null(env)) {
31+
attach_plot_env(env)
3032
}
3133
S7::S7_dispatch()
3234
})
3335

34-
S7::method(ggplot_build, class_ggplot_built) <- function(plot) {
36+
S7::method(build_ggplot, class_ggplot_built) <- function(plot, ...) {
3537
plot # This is a no-op
3638
}
3739

38-
S7::method(ggplot_build, class_ggplot) <- function(plot) {
40+
S7::method(build_ggplot, class_ggplot) <- function(plot, ...) {
3941
plot <- plot_clone(plot)
4042
if (length(plot@layers) == 0) {
4143
plot <- plot + geom_blank()
@@ -132,17 +134,28 @@ S7::method(ggplot_build, class_ggplot) <- function(plot) {
132134
class_ggplot_built(data = data, layout = layout, plot = plot)
133135
}
134136

137+
#' @rdname build_ggplot
135138
#' @export
136-
#' @rdname ggplot_build
139+
ggplot_build <- function(plot, ...) {
140+
UseMethod("ggplot_build")
141+
}
142+
143+
#' @export
144+
ggplot_build.default <- function(plot, ...) {
145+
build_ggplot(plot)
146+
}
147+
148+
#' @export
149+
#' @rdname build_ggplot
137150
get_layer_data <- function(plot = get_last_plot(), i = 1L) {
138151
ggplot_build(plot)@data[[i]]
139152
}
140153
#' @export
141-
#' @rdname ggplot_build
154+
#' @rdname build_ggplot
142155
layer_data <- get_layer_data
143156

144157
#' @export
145-
#' @rdname ggplot_build
158+
#' @rdname build_ggplot
146159
get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
147160
b <- ggplot_build(plot)
148161

@@ -156,19 +169,19 @@ get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
156169
}
157170

158171
#' @export
159-
#' @rdname ggplot_build
172+
#' @rdname build_ggplot
160173
layer_scales <- get_panel_scales
161174

162175
#' @export
163-
#' @rdname ggplot_build
176+
#' @rdname build_ggplot
164177
get_layer_grob <- function(plot = get_last_plot(), i = 1L) {
165178
b <- ggplot_build(plot)
166179

167180
b@plot@layers[[i]]$draw_geom(b@data[[i]], b@layout)
168181
}
169182

170183
#' @export
171-
#' @rdname ggplot_build
184+
#' @rdname build_ggplot
172185
layer_grob <- get_layer_grob
173186

174187
#' Build a plot with all the usual bits and pieces.

man/ggplot_build.Rd renamed to man/build_ggplot.Rd

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

0 commit comments

Comments
 (0)