Skip to content

Commit 32fa918

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

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ S3method(fortify,sfg)
5555
S3method(fortify,summary.glht)
5656
S3method(fortify,tbl)
5757
S3method(fortify,tbl_df)
58+
S3method(ggplot_add,default)
5859
S3method(grid.draw,absoluteGrob)
5960
S3method(grobHeight,absoluteGrob)
6061
S3method(grobHeight,zeroGrob)
@@ -742,6 +743,7 @@ export(transform_position)
742743
export(translate_shape_string)
743744
export(unit)
744745
export(update_geom_defaults)
746+
export(update_ggplot)
745747
export(update_labels)
746748
export(update_stat_defaults)
747749
export(update_theme)

R/plot-construction.R

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,18 @@ add_ggplot <- function(p, object, objectname) {
9797
#' Add custom objects to ggplot
9898
#'
9999
#' This generic allows you to add your own methods for adding custom objects to
100-
#' a ggplot with [+.gg][add_gg].
100+
#' a ggplot with [+.gg][add_gg]. The `ggplot_add()` function is vestigial and
101+
#' the `update_ggplot()` function should be used instead.
101102
#'
102103
#' @param object An object to add to the plot
103104
#' @param plot The ggplot object to add `object` to
104105
#'
105106
#' @return A modified ggplot object
106107
#' @details
107-
#' Custom methods for `ggplot_add()` are intended to update the `plot` variable
108+
#' Custom methods for `update_ggplot()` are intended to update the `plot` variable
108109
#' using information from a custom `object`. This can become convenient when
109110
#' writing extensions that don't build on the pre-existing grammar like
110-
#' layers, facets, coords and themes. The `ggplot_add()` function is never
111+
#' layers, facets, coords and themes. The `update_ggplot()` function is never
111112
#' intended to be used directly, but it is triggered when an object is added
112113
#' to a plot via the `+` operator. Please note that the full `plot` object is
113114
#' exposed at this point, which comes with the responsibility of returning
@@ -118,7 +119,7 @@ add_ggplot <- function(p, object, objectname) {
118119
#' @examples
119120
#' # making a new method for the generic
120121
#' # in this example, we enable adding text elements
121-
#' S7::method(ggplot_add, list(element_text, class_ggplot)) <-
122+
#' S7::method(update_ggplot, list(element_text, class_ggplot)) <-
122123
#' function(object, plot, ...) {
123124
#' plot + theme(text = object)
124125
#' }
@@ -129,37 +130,37 @@ add_ggplot <- function(p, object, objectname) {
129130
#' element_text(colour = "red")
130131
#'
131132
#' # clean-up
132-
ggplot_add <- S7::new_generic("ggplot_add", c("object", "plot"))
133+
update_ggplot <- S7::new_generic("update_ggplot", c("object", "plot"))
133134

134-
S7::method(ggplot_add, list(S7::class_any, class_ggplot)) <-
135+
S7::method(update_ggplot, list(S7::class_any, class_ggplot)) <-
135136
function(object, plot, object_name, ...) {
136137
cli::cli_abort("Can't add {.var {object_name}} to a {.cls ggplot} object.")
137138
}
138139

139-
S7::method(ggplot_add, list(S7::class_function, class_ggplot)) <-
140+
S7::method(update_ggplot, list(S7::class_function, class_ggplot)) <-
140141
function(object, plot, object_name, ...) {
141142
cli::cli_abort(c(
142143
"Can't add {.var {object_name}} to a {.cls ggplot} object",
143144
"i" = "Did you forget to add parentheses, as in {.fn {object_name}}?"
144145
))
145146
}
146147

147-
S7::method(ggplot_add, list(NULL, class_ggplot)) <-
148+
S7::method(update_ggplot, list(NULL, class_ggplot)) <-
148149
function(object, plot, ...) { plot }
149150

150-
S7::method(ggplot_add, list(S7::class_data.frame, class_ggplot)) <-
151+
S7::method(update_ggplot, list(S7::class_data.frame, class_ggplot)) <-
151152
function(object, plot, ...) { S7::set_props(plot, data = object) }
152153

153-
S7::method(ggplot_add, list(class_scale, class_ggplot)) <-
154+
S7::method(update_ggplot, list(class_scale, class_ggplot)) <-
154155
function(object, plot, ...) {
155156
plot@scales$add(object)
156157
plot
157158
}
158159

159-
S7::method(ggplot_add, list(class_labels, class_ggplot)) <-
160+
S7::method(update_ggplot, list(class_labels, class_ggplot)) <-
160161
function(object, plot, ...) { update_labels(plot, object) }
161162

162-
S7::method(ggplot_add, list(class_guides, class_ggplot)) <-
163+
S7::method(update_ggplot, list(class_guides, class_ggplot)) <-
163164
function(object, plot, ...) {
164165
old <- plot@guides
165166
new <- ggproto(NULL, old)
@@ -168,17 +169,17 @@ S7::method(ggplot_add, list(class_guides, class_ggplot)) <-
168169
plot
169170
}
170171

171-
S7::method(ggplot_add, list(class_mapping, class_ggplot)) <-
172+
S7::method(update_ggplot, list(class_mapping, class_ggplot)) <-
172173
function(object, plot, ...) {
173174
S7::set_props(plot, mapping = class_mapping(defaults(object, plot@mapping)))
174175
}
175176

176-
S7::method(ggplot_add, list(class_theme, class_ggplot)) <-
177+
S7::method(update_ggplot, list(class_theme, class_ggplot)) <-
177178
function(object, plot, ...) {
178179
S7::set_props(plot, theme = add_theme(plot@theme, object))
179180
}
180181

181-
S7::method(ggplot_add, list(class_coord, class_ggplot)) <-
182+
S7::method(update_ggplot, list(class_coord, class_ggplot)) <-
182183
function(object, plot, ...) {
183184
if (!isTRUE(plot@coordinates$default)) {
184185
cli::cli_inform(c(
@@ -189,29 +190,42 @@ S7::method(ggplot_add, list(class_coord, class_ggplot)) <-
189190
S7::set_props(plot, coordinates = object)
190191
}
191192

192-
S7::method(ggplot_add, list(class_facet, class_ggplot)) <-
193+
S7::method(update_ggplot, list(class_facet, class_ggplot)) <-
193194
function(object, plot, ...) { S7::set_props(plot, facet = object) }
194195

195-
S7::method(ggplot_add, list(class_layer, class_ggplot)) <-
196+
S7::method(update_ggplot, list(class_layer, class_ggplot)) <-
196197
function(object, plot, ...) {
197198
layers_names <- new_layer_names(object, names2(plot@layers))
198199
object <- setNames(append(plot@layers, object), layers_names)
199200
S7::set_props(plot, layers = object)
200201
}
201202

202-
S7::method(ggplot_add, list(S7::class_list, class_ggplot)) <-
203+
S7::method(update_ggplot, list(S7::class_list, class_ggplot)) <-
203204
function(object, plot, object_name, ...) {
204205
for (o in object) {
205206
plot <- ggplot_add(o, plot, object_name)
206207
}
207208
plot
208209
}
209210

210-
S7::method(ggplot_add, list(S7::new_S3_class("by"), class_ggplot)) <-
211+
S7::method(update_ggplot, list(S7::new_S3_class("by"), class_ggplot)) <-
211212
function(object, plot, object_name, ...) {
212213
ggplot_add(unclass(object), plot, object_name)
213214
}
214215

216+
# For backward compatibility, ggplot_add still exists but by default it wraps
217+
# `update_ggplot()`
218+
#' @rdname update_ggplot
219+
#' @export
220+
ggplot_add <- function(object, plot, ...) {
221+
UseMethod("ggplot_add")
222+
}
223+
224+
#' @export
225+
ggplot_add.default <- function(object, plot, ...) {
226+
update_ggplot(object = object, plot = plot, ...)
227+
}
228+
215229
new_layer_names <- function(layer, existing) {
216230

217231
empty <- !nzchar(existing)

man/ggplot_add.Rd renamed to man/update_ggplot.Rd

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

0 commit comments

Comments
 (0)