@@ -97,17 +97,18 @@ add_ggplot <- function(p, object, objectname) {
97
97
# ' Add custom objects to ggplot
98
98
# '
99
99
# ' 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.
101
102
# '
102
103
# ' @param object An object to add to the plot
103
104
# ' @param plot The ggplot object to add `object` to
104
105
# '
105
106
# ' @return A modified ggplot object
106
107
# ' @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
108
109
# ' using information from a custom `object`. This can become convenient when
109
110
# ' 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
111
112
# ' intended to be used directly, but it is triggered when an object is added
112
113
# ' to a plot via the `+` operator. Please note that the full `plot` object is
113
114
# ' exposed at this point, which comes with the responsibility of returning
@@ -118,7 +119,7 @@ add_ggplot <- function(p, object, objectname) {
118
119
# ' @examples
119
120
# ' # making a new method for the generic
120
121
# ' # 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)) <-
122
123
# ' function(object, plot, ...) {
123
124
# ' plot + theme(text = object)
124
125
# ' }
@@ -129,37 +130,37 @@ add_ggplot <- function(p, object, objectname) {
129
130
# ' element_text(colour = "red")
130
131
# '
131
132
# ' # clean-up
132
- ggplot_add <- S7 :: new_generic(" ggplot_add " , c(" object" , " plot" ))
133
+ update_ggplot <- S7 :: new_generic(" update_ggplot " , c(" object" , " plot" ))
133
134
134
- S7 :: method(ggplot_add , list (S7 :: class_any , class_ggplot )) <-
135
+ S7 :: method(update_ggplot , list (S7 :: class_any , class_ggplot )) <-
135
136
function (object , plot , object_name , ... ) {
136
137
cli :: cli_abort(" Can't add {.var {object_name}} to a {.cls ggplot} object." )
137
138
}
138
139
139
- S7 :: method(ggplot_add , list (S7 :: class_function , class_ggplot )) <-
140
+ S7 :: method(update_ggplot , list (S7 :: class_function , class_ggplot )) <-
140
141
function (object , plot , object_name , ... ) {
141
142
cli :: cli_abort(c(
142
143
" Can't add {.var {object_name}} to a {.cls ggplot} object" ,
143
144
" i" = " Did you forget to add parentheses, as in {.fn {object_name}}?"
144
145
))
145
146
}
146
147
147
- S7 :: method(ggplot_add , list (NULL , class_ggplot )) <-
148
+ S7 :: method(update_ggplot , list (NULL , class_ggplot )) <-
148
149
function (object , plot , ... ) { plot }
149
150
150
- S7 :: method(ggplot_add , list (S7 :: class_data.frame , class_ggplot )) <-
151
+ S7 :: method(update_ggplot , list (S7 :: class_data.frame , class_ggplot )) <-
151
152
function (object , plot , ... ) { S7 :: set_props(plot , data = object ) }
152
153
153
- S7 :: method(ggplot_add , list (class_scale , class_ggplot )) <-
154
+ S7 :: method(update_ggplot , list (class_scale , class_ggplot )) <-
154
155
function (object , plot , ... ) {
155
156
plot @ scales $ add(object )
156
157
plot
157
158
}
158
159
159
- S7 :: method(ggplot_add , list (class_labels , class_ggplot )) <-
160
+ S7 :: method(update_ggplot , list (class_labels , class_ggplot )) <-
160
161
function (object , plot , ... ) { update_labels(plot , object ) }
161
162
162
- S7 :: method(ggplot_add , list (class_guides , class_ggplot )) <-
163
+ S7 :: method(update_ggplot , list (class_guides , class_ggplot )) <-
163
164
function (object , plot , ... ) {
164
165
old <- plot @ guides
165
166
new <- ggproto(NULL , old )
@@ -168,17 +169,17 @@ S7::method(ggplot_add, list(class_guides, class_ggplot)) <-
168
169
plot
169
170
}
170
171
171
- S7 :: method(ggplot_add , list (class_mapping , class_ggplot )) <-
172
+ S7 :: method(update_ggplot , list (class_mapping , class_ggplot )) <-
172
173
function (object , plot , ... ) {
173
174
S7 :: set_props(plot , mapping = class_mapping(defaults(object , plot @ mapping )))
174
175
}
175
176
176
- S7 :: method(ggplot_add , list (class_theme , class_ggplot )) <-
177
+ S7 :: method(update_ggplot , list (class_theme , class_ggplot )) <-
177
178
function (object , plot , ... ) {
178
179
S7 :: set_props(plot , theme = add_theme(plot @ theme , object ))
179
180
}
180
181
181
- S7 :: method(ggplot_add , list (class_coord , class_ggplot )) <-
182
+ S7 :: method(update_ggplot , list (class_coord , class_ggplot )) <-
182
183
function (object , plot , ... ) {
183
184
if (! isTRUE(plot @ coordinates $ default )) {
184
185
cli :: cli_inform(c(
@@ -189,29 +190,42 @@ S7::method(ggplot_add, list(class_coord, class_ggplot)) <-
189
190
S7 :: set_props(plot , coordinates = object )
190
191
}
191
192
192
- S7 :: method(ggplot_add , list (class_facet , class_ggplot )) <-
193
+ S7 :: method(update_ggplot , list (class_facet , class_ggplot )) <-
193
194
function (object , plot , ... ) { S7 :: set_props(plot , facet = object ) }
194
195
195
- S7 :: method(ggplot_add , list (class_layer , class_ggplot )) <-
196
+ S7 :: method(update_ggplot , list (class_layer , class_ggplot )) <-
196
197
function (object , plot , ... ) {
197
198
layers_names <- new_layer_names(object , names2(plot @ layers ))
198
199
object <- setNames(append(plot @ layers , object ), layers_names )
199
200
S7 :: set_props(plot , layers = object )
200
201
}
201
202
202
- S7 :: method(ggplot_add , list (S7 :: class_list , class_ggplot )) <-
203
+ S7 :: method(update_ggplot , list (S7 :: class_list , class_ggplot )) <-
203
204
function (object , plot , object_name , ... ) {
204
205
for (o in object ) {
205
206
plot <- ggplot_add(o , plot , object_name )
206
207
}
207
208
plot
208
209
}
209
210
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 )) <-
211
212
function (object , plot , object_name , ... ) {
212
213
ggplot_add(unclass(object ), plot , object_name )
213
214
}
214
215
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
+
215
229
new_layer_names <- function (layer , existing ) {
216
230
217
231
empty <- ! nzchar(existing )
0 commit comments