1
1
# ' Build ggplot for rendering.
2
2
# '
3
- # ' `ggplot_build ()` takes the plot object, and performs all steps necessary
3
+ # ' `build_ggplot ()` takes the plot object, and performs all steps necessary
4
4
# ' to produce an object that can be rendered. This function outputs two pieces:
5
5
# ' 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.
7
8
# '
8
9
# ' `get_layer_data()`, `get_layer_grob()`, and `get_panel_scales()` are helper
9
10
# ' functions that return the data, grob, or scales associated with a given
15
16
# ' plot). In `get_panel_scales()`, the row of a facet to return scales for.
16
17
# ' @param j An integer. In `get_panel_scales()`, the column of a facet to return
17
18
# ' scales for.
19
+ # ' @param ... Not currently in use.
18
20
# ' @seealso
19
21
# ' [print.ggplot()] and [benchplot()] for
20
22
# ' functions that contain the complete set of steps for generating
23
25
# ' The `r link_book("build step section", "internals#sec-ggplotbuild")`
24
26
# ' @keywords internal
25
27
# ' @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 )
30
32
}
31
33
S7 :: S7_dispatch()
32
34
})
33
35
34
- S7 :: method(ggplot_build , class_ggplot_built ) <- function (plot ) {
36
+ S7 :: method(build_ggplot , class_ggplot_built ) <- function (plot , ... ) {
35
37
plot # This is a no-op
36
38
}
37
39
38
- S7 :: method(ggplot_build , class_ggplot ) <- function (plot ) {
40
+ S7 :: method(build_ggplot , class_ggplot ) <- function (plot , ... ) {
39
41
plot <- plot_clone(plot )
40
42
if (length(plot @ layers ) == 0 ) {
41
43
plot <- plot + geom_blank()
@@ -132,17 +134,28 @@ S7::method(ggplot_build, class_ggplot) <- function(plot) {
132
134
class_ggplot_built(data = data , layout = layout , plot = plot )
133
135
}
134
136
137
+ # ' @rdname build_ggplot
135
138
# ' @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
137
150
get_layer_data <- function (plot = get_last_plot(), i = 1L ) {
138
151
ggplot_build(plot )@ data [[i ]]
139
152
}
140
153
# ' @export
141
- # ' @rdname ggplot_build
154
+ # ' @rdname build_ggplot
142
155
layer_data <- get_layer_data
143
156
144
157
# ' @export
145
- # ' @rdname ggplot_build
158
+ # ' @rdname build_ggplot
146
159
get_panel_scales <- function (plot = get_last_plot(), i = 1L , j = 1L ) {
147
160
b <- ggplot_build(plot )
148
161
@@ -156,19 +169,19 @@ get_panel_scales <- function(plot = get_last_plot(), i = 1L, j = 1L) {
156
169
}
157
170
158
171
# ' @export
159
- # ' @rdname ggplot_build
172
+ # ' @rdname build_ggplot
160
173
layer_scales <- get_panel_scales
161
174
162
175
# ' @export
163
- # ' @rdname ggplot_build
176
+ # ' @rdname build_ggplot
164
177
get_layer_grob <- function (plot = get_last_plot(), i = 1L ) {
165
178
b <- ggplot_build(plot )
166
179
167
180
b @ plot @ layers [[i ]]$ draw_geom(b @ data [[i ]], b @ layout )
168
181
}
169
182
170
183
# ' @export
171
- # ' @rdname ggplot_build
184
+ # ' @rdname build_ggplot
172
185
layer_grob <- get_layer_grob
173
186
174
187
# ' Build a plot with all the usual bits and pieces.
0 commit comments