File: Layer-class.Rd

package info (click to toggle)
r-cran-ggplot2 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,084 kB
  • sloc: sh: 15; makefile: 5
file content (437 lines) | stat: -rw-r--r-- 11,366 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/layer.R
\docType{data}
\name{Layer-class}
\alias{Layer-class}
\alias{Layer}
\title{Layers}
\description{
The Layer class is a chaperone class not available for extension. The class
fulfils the following tasks. The class houses the Geom, Stat and Position
trinity and tracks their stateful parameters. Furthermore, its methods are
responsible for managing the layer data and exposing it to other components
of the plot at the right time.
}
\details{
The Layer class is an internal class that is not exported because the class
is not intended for extension. The \code{layer()} function instantiates the
LayerInstance class, which inherits from Layer, but has relevant fields
populated.

The class is mostly used in \code{ggplot_build()}, with the notable exception
of the \code{draw_geom()} method, which is used in \code{ggplot_gtable()} instead.
}
\section{Fields}{

\describe{
\item{\code{constructor}}{A \link[=call]{call} object with the user-facing
constructor function, for use in error messaging. This field is populated
by \code{layer()}.}

\item{\code{geom,stat,position}}{These fields house the Geom, Stat and Position
trifecta in ggproto form and is populated by \code{layer()}.}

\item{\code{stat_params,computed_stat_params}}{These fields hold parameters
assigned to the Stat. The \code{stat_params} field is directly derived from
user input and is populated by \code{layer()}. The \code{computed_stat_params}
carries state and is constructed by the \code{Stat$setup_params()} method.}

\item{\code{geom_params,computed_geom_params}}{These fields hold parameters
assigned to the Geom. The \code{geom_params} field is directly derived from
user input and is populated by \code{layer()}. The \code{computed_geom_params}
carries state and is constructed by the \code{Geom$setup_params()} method.}

\item{\code{mapping,computed_mapping}}{These fields hold \link[=aes]{mapping}s.
The \code{mapping} field holds the \code{layer(mapping)} argument. The
\code{computed_mapping} field carries state and is constructed in the
\code{setup_layer()} method.}

\item{\code{data}}{The fortified \code{layer(data)} argument.}

\item{\code{aes_params}}{Holds the fixed, unmapped aesthetics passed to
\code{layer(params)} as determined by \code{Geom$aesthetics()}.}

\item{\code{inherit.aes}}{A scalar boolean used in the \code{setup_layer()} method to
indicate whether the \code{computed_mapping} should include the global mapping
(\code{TRUE}) or only the layer mapping (\code{FALSE}). This is populated by the
\code{layer(inherit.aes)} parameter.}

\item{\code{layer_data}}{\strong{Description}

A function method for initially resolving layer data. If layer data is
missing or is a function, it will derive layer data from the global plot
data.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$layer_data(plot_data)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{plot_data}}{The \code{data} field of the ggplot object.}
}

\strong{Value}

A data frame with layer data or \code{NULL}}

\item{\code{setup_layer}}{\strong{Description}

A function method is a hook to allow a final access to layer data in
input form. In addition, it allows a layer access to global plot
information. The latter is used to enforce the \code{inherit.aes} parameter by
supplementing the layer mapping with the global mapping when requested.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$setup_data(data, plot)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{plot}}{A ggplot object}
}

\strong{Value}

A data frame with layer data. As a side effect, the \code{computed_mapping}
field is populated.}

\item{\code{compute_aesthetics}}{\strong{Description}

A function method that evaluates aesthetics and warns about any problems.
It also infers a \code{group} aesthetic if not provided. This method is also
the step where layer data becomes standardised to base data frames without
row names or additional attributes.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$compute_aesthetics(data, plot)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{plot}}{A ggplot object}
}

\strong{Value}

A data frame with layer data}

\item{\code{compute_aesthetics}}{\strong{Description}

A function method that orchestrates computing statistics. It executes
methods from the Stat class to form new computed variables.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$compute_statistic(data, layout)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{layout}}{A \verb{<Layout>} ggproto object.}
}

\strong{Value}

A data frame with layer data. As a side effect the \code{computed_stat_params}
field is populated.}

\item{\code{map_statistic}}{\strong{Description}

A function method that finishes the result of computed statistics. It has
several tasks:
\itemize{
\item It evaluates the \code{after_stat()} stage of the mapping from both the
\code{computed_mapping} but also the \code{Stat$default_aes} fields.
\item It ensures relevant scales are instantiated for computed aesthetics.
\item It takes care that scale transformation is applied to computed aesthetics.
}

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$map_statistic(data, plot)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{plot}}{A ggplot object.}
}

\strong{Value}

A data frame with layer data}

\item{\code{compute_geom_1}}{\strong{Description}

A function method that prepares data for drawing. It checks that all
required aesthetics are present and sets up parameters and data using the
Geom class.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$compute_geom_1(data)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
}

\strong{Value}

A data frame with layer data. As a side effect the \code{computed_geom_params}
field is populated.}

\item{\code{compute_position}}{\strong{Description}

A function method that orchestrates the position adjustment. It executes
methods from the Position class.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$compute_position(data, layout)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{layout}}{A \verb{<Layout>} ggproto object.}
}

\strong{Value}

A data frame with layer data.}

\item{\code{compute_geom_2}}{\strong{Description}

A function method that add defaults and fixed parameters. It wraps the
\code{Geom$use_defaults()} method.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$compute_geom_2(data, params, theme, ...)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{params}}{A list with fixed aesthetic parameters, typically the
\code{aes_params} field.}
\item{\code{theme}}{A \link[=theme]{theme} object}
\item{\code{...}}{Passed on to \code{Geom$use_defaults()}, not in use.}
}

\strong{Value}

A data frame with layer data.}

\item{\code{finish_statistics}}{\strong{Description}

A function method that wraps \code{Stat$finish_layer()}.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$finish_statistics(data)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
}

\strong{Value}

A data frame with layer data.}

\item{\code{draw_geom}}{\strong{Description}

A function method that produces graphics for every panel. It uses Geom
class methods to handle missing data and produce grobs. In contrast to
other methods, this is called during the \code{ggplot_gtable()} stage, not the
\code{ggplot_build()} stage.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$draw_geom(data, layout)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{layout}}{A \verb{<Layout>} ggproto object.}
}

\strong{Value}

A list of grobs, one per panel.}

\item{\code{print}}{\strong{Description}

A function method that prints information about the layer.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Layer$print()
}\if{html}{\out{</div>}}

\strong{Value}

Nothing (\code{NULL}), invisibly}
}}

\section{Layer data diagram}{


As the Layer class is a chaparone for the data, it makes sense to give a
small overview of how layer data flows through a plot. In the diagram below
we following the \code{layer(data)} argument over the course of plot building
through Layer class methods. When an outside class acts on the data without
the Layer class, this is indicated with the left arrow \verb{<-}. Subcomponents
of a method that touch data are indicated with the right arrow \verb{->}.

\if{html}{\out{<div class="sourceCode r">}}\preformatted{# Inside `ggplot_build()`
 |
layer(data)
 |
 |
 | # Inherit plot data
 |
Layer$layer_data()
 |
 |
 | # Finalise mapping
 |
Layer$setup_layer()
 |
 |
 | # Append PANEL variable for facets
 |
 |<- Layout$setup()
 |    |
 |    +-> Facet$setup_data()
 |    |
 |    +-> Coord$setup_data()
 |
 |
 | # Evaluate mappings to new data and infer group
 |
Layer$compute_aesthetics()
 |
 |
 | # Scale-transform all aesthetics
 |
 |<- ScalesList$transform_df()
 |    |
 |    +-> Scale$transform_df()
 |
 |
 | # Map x/y aesthetics with initial scale
 |
 |<- Layout$map_position()
 |    |
 |    +-> Scale$map()
 |
 |
 | # Compute stat part of layer
 |
Layer$compute_statistic()
 | |
 | +-> Stat$setup_data()
 | |
 | +-> Stat$compute_layer()
 |
 |
 | # Add `after_stat()` stage
 | # Scale transform computed variables
 |
Layer$map_statistic()
 |
 |
 | # Setup geom part of layer
 |
Layer$compute_geom_1()
 | |
 | +-> Geom$setup_data()
 |
 |
 | # Apply position adjustments
 |
Layer$compute_position()
 | |
 | +-> Position$use_defaults()
 | |
 | +-> Position$setup_data()
 | |
 | +-> Position$compute_layer()
 |
 |
 | # Map x/y aesthetics with final scales
 |
 |<- Layout$map_position()
 |    |
 |    +-> Scale$map()
 |
 | # Map non-position aesthetics
 |
 |<- ScalesList$map_df()
 |    |
 |    +-> Scale$map()
 |
 |
 | # Fill in defaults and fixed aesthetics
 |
Layer$compute_geom_2()
 | |
 | +-> Geom$use_defaults()
 |
 |
 | # Apply final Stat hook
 |
Layer$finish_statistics()
 | |
 | +-> Stat$finish_layer()
 |
 |
 | # Apply final Facet hook
 |
 |<- Layout$finish_data()
 |    |
 |    +-> Facet$finish_data()
 |
 V
# `ggplot_build()` is finished
# Hand off to `ggplot_gtable()`
 |
 |
 | # Draw the geom part
 |
Layer$draw_geom()
 |
 +-> Geom$handle_na()
 |
 +-> Geom$draw_layer()
}\if{html}{\out{</div>}}
}

\examples{
# None: Layer is not intended to be extended
}
\seealso{
Other Layer components: 
\code{\link{Geom}},
\code{\link{Position}},
\code{\link{Stat}}

Other chaperone classes: 
\code{\link{Layout}}
}
\concept{Layer components}
\concept{chaperone classes}
\keyword{internal}