File: Stat.Rd

package info (click to toggle)
r-cran-ggplot2 4.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,084 kB
  • sloc: sh: 15; makefile: 5
file content (301 lines) | stat: -rw-r--r-- 9,659 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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stat-.R, R/stat-align.R, R/stat-bin.R,
%   R/stat-summary-2d.R, R/stat-bin2d.R, R/stat-bindot.R, R/stat-binhex.R,
%   R/stat-boxplot.R, R/stat-connect.R, R/stat-contour.R, R/stat-count.R,
%   R/stat-density-2d.R, R/stat-density.R, R/stat-ecdf.R, R/stat-ellipse.R,
%   R/stat-function.R, R/stat-identity.R, R/stat-manual.R, R/stat-qq-line.R,
%   R/stat-qq.R, R/stat-quantilemethods.R, R/stat-smooth.R, R/stat-sum.R,
%   R/stat-summary-bin.R, R/stat-summary-hex.R, R/stat-summary.R,
%   R/stat-unique.R, R/stat-ydensity.R
\docType{data}
\name{Stat}
\alias{Stat}
\alias{StatAlign}
\alias{StatBin}
\alias{StatSummary2d}
\alias{StatBin2d}
\alias{StatBindot}
\alias{StatBinhex}
\alias{StatBoxplot}
\alias{StatConnect}
\alias{StatContour}
\alias{StatContourFilled}
\alias{StatCount}
\alias{StatDensity2d}
\alias{StatDensity2dFilled}
\alias{StatDensity}
\alias{StatEcdf}
\alias{StatEllipse}
\alias{StatFunction}
\alias{StatIdentity}
\alias{StatManual}
\alias{StatQqLine}
\alias{StatQq}
\alias{StatQuantile}
\alias{StatSmooth}
\alias{StatSum}
\alias{StatSummaryBin}
\alias{StatSummaryHex}
\alias{StatSummary}
\alias{StatUnique}
\alias{StatYdensity}
\title{Stats}
\description{
All \verb{stat_*()} functions (like \code{stat_bin()}) return a layer that
contains a \verb{Stat*} object (like \code{StatBin}). The \verb{Stat*}
object is responsible for rendering the data in the plot.
}
\details{
Each of the \verb{Stat*} objects is a \code{\link[=ggproto]{ggproto()}} object, descended
from the top-level \code{Stat}, and each implements various methods and
fields. The object and its parameters are chaperoned by the \link{Layer} class.

To create a new type of Stat object, you typically will want to
override one or more of the following:
\itemize{
\item The \code{required_aes} and \code{default_aes} fields.
\item One of the \code{compute_layer()}, \code{compute_panel()} or \code{compute_group()}
functions. Typically it best to implement \code{compute_group()} and use the
higher-up methods when there are substantial performance improvements to
be gained.
\item The \code{finish_layer()} method
}
}
\section{Fields}{

\describe{
\item{\code{required_aes}}{A character vector naming aesthetics that are necessary
to compute the stat.}

\item{\code{non_missing_aes}}{A character vector naming aesthetics that will cause
removal if they have missing values.}

\item{\code{optional_aes}}{A character vector naming aesthetics that will be
accepted by \code{layer()}, but are not required or dscribed in the \code{default_aes}
field.}

\item{\code{default_aes}}{A \link[=aes]{mapping} of default values for aesthetics.
Aesthetics can be set to \code{NULL} to be included as optional aesthetic.}

\item{\code{dropped_aes}}{A character vector naming aesthetics that can be dropped
from the data without warning. Typically used for aesthetics that are
'consumed' during computation like \code{"weight"}.}

\item{\code{extra_params}}{A character vector of parameter names in addition to
those imputed from the \code{compute_panel()} or \code{compute_groups()} methods.
This field can be set to include parameters for \code{setup_data()} methods.
By default, this only contains \code{"na.rm"}.}

\item{\code{retransform}}{A scalar boolean: should the values produced by the
statistic also be transformed in the second pass when recently added
statistics are trained to the scales}

\item{\code{setup_params}}{\strong{Description}

A function method for modifying or checking the parameters based on the
data. The default method returns the parameters unaltered.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Stat$setup_params(data, params)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{params}}{A list of current parameters}
}

\strong{Value}

A list of parameters}

\item{\code{setup_data}}{\strong{Description}

A function method for modifying or checking the data. The default method
returns data unaltered.

\strong{Usage}

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

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{params}}{A list of parameters coming from the \code{setup_params()}
method}
}

\strong{Value}

A data frame with layer data}

\item{\code{compute_layer}}{\strong{Description}

A function method for orchestrating the computation of the statistic. The
default method splits the data and passes on computation tasks to the
panel-level \code{compute_panel()} method. In addition, the default method
handles missing values by removing rows that have missing values for the
aesthetics listed in the \code{required_aes} and \code{non_missing_aes} fields. It is
not recommended to use this method as an extension point.

\strong{Usage}

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

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{params}}{A list of parameters}
\item{\code{layout}}{A pre-trained \verb{<Layout>} ggproto object.}
}

\strong{Value}

A data frame with computed data}

\item{\code{compute_panel,compute_group}}{\strong{Description}

A function method orchestrating the computation of statistics for a single
panel or group. The default \code{compute_panel()} method splits the data into
groups, and passes on computation tasks to the \code{compute_group()} method.
In addition, \code{compute_panel()} is tasked with preserving aesthetics that
are constant within a group and preserving these if the computation loses
them. The default \code{compute_group()} is not implemented.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Stat$compute_panel(data, scales, ...)
Stat$compute_group(data, scales, ...)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with the layer's data.}
\item{\code{scales}}{A list of pre-trained \code{x} and \code{y} scales. Note that the
position scales are not finalised at this point and reflect the initial
data range before computing stats.}
\item{\code{...}}{Reserved for extensions. By default, this passes parameters
to the \code{compute_group()} method.}
}

\strong{Value}

A data frame with layer data}

\item{\code{finish_layer}}{\strong{Description}

A function method acting as a hook to modify data after scales have been
applied, but before geoms have to render. The default is to pass the data
unaltered. This can be used as an extension point when actual aesthetic
values rather than values mapped to the aesthetic are needed.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Stat$finish_layer(data, params)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{data}}{A data frame with layer data}
\item{\code{params}}{A list of parameters}
}

\strong{Value}

A data frame with layer data}

\item{\code{parameters}}{\strong{Description}

A function method for listing out all acceptable parameters for this stat.

\strong{Usage}

\if{html}{\out{<div class="sourceCode r">}}\preformatted{Stat$parameters(extra)
}\if{html}{\out{</div>}}

\strong{Arguments}
\describe{
\item{\code{extra}}{A boolean: whether to include the \code{extra_params} field.}
}

\strong{Value}

A character vector of parameter names.}

\item{\code{aesthetics}}{\strong{Description}

A function method for listing out all acceptable aesthetics for this stat.

\strong{Usage}

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

\strong{Value}

A character vector of aesthetic names.}
}}

\section{Conventions}{


The object name that a new class is assigned to is typically the same as the
class name. Stat class names are in UpperCamelCase and start with the \verb{Stat*}
prefix, like \code{StatNew}.

A constructor function is usually paired wih a Stat class. The constructor
wraps a call to \code{layer()}, where e.g. \code{layer(stat = StatNew)}. The
constructor function name is formatted by taking the Stat class name and
formatting it with snake_case, so that \code{StatNew} becomes \code{stat_new()}.
}

\examples{
# Extending the class
StatKmeans <- ggproto(
  "StatKmeans", Stat,
  # Fields
  required_aes = c("x", "y"),
  # You can relate computed variables to aesthetics using `after_stat()`
  # in defaults
  default_aes = aes(colour = after_stat(cluster)),
  # Methods
  compute_panel = function(data, scales, k = 2L) {
    km <- kmeans(cbind(scale(data$x), scale(data$y)), centers = k)
    data$cluster <- factor(km$cluster)
    data
  }
)

# Building a constructor
stat_kmeans <- function(mapping = NULL, data = NULL, geom = "point",
                        position = "identity", ..., k = 2L, na.rm = FALSE,
                        show.legend = NA, inherit.aes = TRUE) {
  layer(
    mapping = mapping, data = data,
    geom = geom, stat = StatKmeans, position = position,
    show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(na.rm = na.rm, k = k, ...)
  )
}

# Use new stat in plot
ggplot(mpg, aes(displ, hwy)) +
  stat_kmeans(k = 3)
}
\seealso{
The \href{https://ggplot2-book.org/extensions#sec-new-stats}{new stats section} of the online ggplot2 book..

Run \code{vignette("extending-ggplot2")}, in particular the "Creating a
new stat" section.

Other Layer components: 
\code{\link{Geom}},
\code{\link{Layer-class}},
\code{\link{Position}}
}
\concept{Layer components}
\keyword{datasets}
\keyword{internal}