Skip to content

Commit 4dd78b5

Browse files
committed
use the make_constructor() for Stats
1 parent cd54bfb commit 4dd78b5

24 files changed

+668
-1307
lines changed

R/stat-align.R

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,9 @@
1-
#' @inheritParams layer
2-
#' @inheritParams geom_point
3-
#' @export
4-
#' @rdname geom_ribbon
5-
stat_align <- function(mapping = NULL, data = NULL,
6-
geom = "area", position = "identity",
7-
...,
8-
na.rm = FALSE,
9-
show.legend = NA,
10-
inherit.aes = TRUE) {
11-
layer(
12-
data = data,
13-
mapping = mapping,
14-
stat = StatAlign,
15-
geom = geom,
16-
position = position,
17-
show.legend = show.legend,
18-
inherit.aes = inherit.aes,
19-
params = list2(
20-
na.rm = na.rm,
21-
...
22-
)
23-
)
24-
}
25-
261
#' @rdname ggplot2-ggproto
272
#' @format NULL
283
#' @usage NULL
294
#' @export
30-
StatAlign <- ggproto("StatAlign", Stat,
5+
StatAlign <- ggproto(
6+
"StatAlign", Stat,
317
extra_params = c("na.rm", "orientation"),
328
required_aes = c("x", "y"),
339

@@ -99,3 +75,12 @@ StatAlign <- ggproto("StatAlign", Stat,
9975
flip_data(data_aligned, flipped_aes)
10076
}
10177
)
78+
79+
#' @inheritParams layer
80+
#' @inheritParams geom_point
81+
#' @export
82+
#' @rdname geom_ribbon
83+
stat_align <- make_constructor(
84+
StatAlign, geom = "area",
85+
exclude = c("unique_loc", "adjust")
86+
)

R/stat-bin.R

Lines changed: 52 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,9 @@
1-
#' @param binwidth The width of the bins. Can be specified as a numeric value
2-
#' or as a function that takes x after scale transformation as input and
3-
#' returns a single numeric value. When specifying a function along with a
4-
#' grouping structure, the function will be called once per group.
5-
#' The default is to use the number of bins in `bins`,
6-
#' covering the range of the data. You should always override
7-
#' this value, exploring multiple widths to find the best to illustrate the
8-
#' stories in your data.
9-
#'
10-
#' The bin width of a date variable is the number of days in each time; the
11-
#' bin width of a time variable is the number of seconds.
12-
#' @param bins Number of bins. Overridden by `binwidth`. Defaults to 30.
13-
#' @param center,boundary bin position specifiers. Only one, `center` or
14-
#' `boundary`, may be specified for a single plot. `center` specifies the
15-
#' center of one of the bins. `boundary` specifies the boundary between two
16-
#' bins. Note that if either is above or below the range of the data, things
17-
#' will be shifted by the appropriate integer multiple of `binwidth`.
18-
#' For example, to center on integers use `binwidth = 1` and `center = 0`, even
19-
#' if `0` is outside the range of the data. Alternatively, this same alignment
20-
#' can be specified with `binwidth = 1` and `boundary = 0.5`, even if `0.5` is
21-
#' outside the range of the data.
22-
#' @param breaks Alternatively, you can supply a numeric vector giving
23-
#' the bin boundaries. Overrides `binwidth`, `bins`, `center`,
24-
#' and `boundary`. Can also be a function that takes group-wise values as input and returns bin boundaries.
25-
#' @param closed One of `"right"` or `"left"` indicating whether right
26-
#' or left edges of bins are included in the bin.
27-
#' @param pad If `TRUE`, adds empty bins at either end of x. This ensures
28-
#' frequency polygons touch 0. Defaults to `FALSE`.
29-
#' @eval rd_computed_vars(
30-
#' count = "number of points in bin.",
31-
#' density = "density of points in bin, scaled to integrate to 1.",
32-
#' ncount = "count, scaled to a maximum of 1.",
33-
#' ndensity = "density, scaled to a maximum of 1.",
34-
#' width = "widths of bins."
35-
#' )
36-
#'
37-
#' @section Dropped variables:
38-
#' \describe{
39-
#' \item{`weight`}{After binning, weights of individual data points (if supplied) are no longer available.}
40-
#' }
41-
#'
42-
#' @seealso [stat_count()], which counts the number of cases at each x
43-
#' position, without binning. It is suitable for both discrete and continuous
44-
#' x data, whereas `stat_bin()` is suitable only for continuous x data.
45-
#' @export
46-
#' @rdname geom_histogram
47-
stat_bin <- function(mapping = NULL, data = NULL,
48-
geom = "bar", position = "stack",
49-
...,
50-
binwidth = NULL,
51-
bins = NULL,
52-
center = NULL,
53-
boundary = NULL,
54-
breaks = NULL,
55-
closed = c("right", "left"),
56-
pad = FALSE,
57-
na.rm = FALSE,
58-
orientation = NA,
59-
show.legend = NA,
60-
inherit.aes = TRUE) {
61-
62-
layer(
63-
data = data,
64-
mapping = mapping,
65-
stat = StatBin,
66-
geom = geom,
67-
position = position,
68-
show.legend = show.legend,
69-
inherit.aes = inherit.aes,
70-
params = list2(
71-
binwidth = binwidth,
72-
bins = bins,
73-
center = center,
74-
boundary = boundary,
75-
breaks = breaks,
76-
closed = closed,
77-
pad = pad,
78-
na.rm = na.rm,
79-
orientation = orientation,
80-
...
81-
)
82-
)
83-
}
84-
851
#' @rdname ggplot2-ggproto
862
#' @format NULL
873
#' @usage NULL
884
#' @export
89-
StatBin <- ggproto("StatBin", Stat,
5+
StatBin <- ggproto(
6+
"StatBin", Stat,
907
setup_params = function(self, data, params) {
918
params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = FALSE)
929

@@ -171,3 +88,53 @@ StatBin <- ggproto("StatBin", Stat,
17188
dropped_aes = "weight" # after statistical transformation, weights are no longer available
17289
)
17390

91+
#' @param binwidth The width of the bins. Can be specified as a numeric value
92+
#' or as a function that takes x after scale transformation as input and
93+
#' returns a single numeric value. When specifying a function along with a
94+
#' grouping structure, the function will be called once per group.
95+
#' The default is to use the number of bins in `bins`,
96+
#' covering the range of the data. You should always override
97+
#' this value, exploring multiple widths to find the best to illustrate the
98+
#' stories in your data.
99+
#'
100+
#' The bin width of a date variable is the number of days in each time; the
101+
#' bin width of a time variable is the number of seconds.
102+
#' @param bins Number of bins. Overridden by `binwidth`. Defaults to 30.
103+
#' @param center,boundary bin position specifiers. Only one, `center` or
104+
#' `boundary`, may be specified for a single plot. `center` specifies the
105+
#' center of one of the bins. `boundary` specifies the boundary between two
106+
#' bins. Note that if either is above or below the range of the data, things
107+
#' will be shifted by the appropriate integer multiple of `binwidth`.
108+
#' For example, to center on integers use `binwidth = 1` and `center = 0`, even
109+
#' if `0` is outside the range of the data. Alternatively, this same alignment
110+
#' can be specified with `binwidth = 1` and `boundary = 0.5`, even if `0.5` is
111+
#' outside the range of the data.
112+
#' @param breaks Alternatively, you can supply a numeric vector giving
113+
#' the bin boundaries. Overrides `binwidth`, `bins`, `center`,
114+
#' and `boundary`. Can also be a function that takes group-wise values as input and returns bin boundaries.
115+
#' @param closed One of `"right"` or `"left"` indicating whether right
116+
#' or left edges of bins are included in the bin.
117+
#' @param pad If `TRUE`, adds empty bins at either end of x. This ensures
118+
#' frequency polygons touch 0. Defaults to `FALSE`.
119+
#' @eval rd_computed_vars(
120+
#' count = "number of points in bin.",
121+
#' density = "density of points in bin, scaled to integrate to 1.",
122+
#' ncount = "count, scaled to a maximum of 1.",
123+
#' ndensity = "density, scaled to a maximum of 1.",
124+
#' width = "widths of bins."
125+
#' )
126+
#'
127+
#' @section Dropped variables:
128+
#' \describe{
129+
#' \item{`weight`}{After binning, weights of individual data points (if supplied) are no longer available.}
130+
#' }
131+
#'
132+
#' @seealso [stat_count()], which counts the number of cases at each x
133+
#' position, without binning. It is suitable for both discrete and continuous
134+
#' x data, whereas `stat_bin()` is suitable only for continuous x data.
135+
#' @export
136+
#' @rdname geom_histogram
137+
stat_bin <- make_constructor(
138+
StatBin, geom = "bar", position = "stack",
139+
orientation = NA
140+
)

R/stat-bin2d.R

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,9 @@
1-
#' @param bins numeric vector giving number of bins in both vertical and
2-
#' horizontal directions. Set to 30 by default.
3-
#' @param binwidth Numeric vector giving bin width in both vertical and
4-
#' horizontal directions. Overrides `bins` if both set.
5-
#' @param drop if `TRUE` removes all cells with 0 counts.
6-
#' @export
7-
#' @rdname geom_bin_2d
8-
#' @eval rd_computed_vars(
9-
#' count = "number of points in bin.",
10-
#' density = "density of points in bin, scaled to integrate to 1.",
11-
#' ncount = "count, scaled to maximum of 1.",
12-
#' ndensity = "density, scaled to a maximum of 1."
13-
#' )
14-
stat_bin_2d <- function(mapping = NULL, data = NULL,
15-
geom = "tile", position = "identity",
16-
...,
17-
bins = 30,
18-
binwidth = NULL,
19-
drop = TRUE,
20-
na.rm = FALSE,
21-
show.legend = NA,
22-
inherit.aes = TRUE) {
23-
layer(
24-
data = data,
25-
mapping = mapping,
26-
stat = StatBin2d,
27-
geom = geom,
28-
position = position,
29-
show.legend = show.legend,
30-
inherit.aes = inherit.aes,
31-
params = list2(
32-
bins = bins,
33-
binwidth = binwidth,
34-
drop = drop,
35-
na.rm = na.rm,
36-
...
37-
)
38-
)
39-
}
40-
41-
42-
#' @export
43-
#' @rdname geom_bin_2d
44-
#' @usage NULL
45-
stat_bin2d <- stat_bin_2d
46-
471
#' @rdname ggplot2-ggproto
482
#' @format NULL
493
#' @usage NULL
504
#' @export
51-
StatBin2d <- ggproto("StatBin2d", Stat,
5+
StatBin2d <- ggproto(
6+
"StatBin2d", Stat,
527
default_aes = aes(weight = 1, fill = after_stat(count)),
538
required_aes = c("x", "y"),
549

@@ -89,6 +44,26 @@ StatBin2d <- ggproto("StatBin2d", Stat,
8944
dropped_aes = "weight" # No longer available after transformation
9045
)
9146

47+
#' @param bins numeric vector giving number of bins in both vertical and
48+
#' horizontal directions. Set to 30 by default.
49+
#' @param binwidth Numeric vector giving bin width in both vertical and
50+
#' horizontal directions. Overrides `bins` if both set.
51+
#' @param drop if `TRUE` removes all cells with 0 counts.
52+
#' @export
53+
#' @rdname geom_bin_2d
54+
#' @eval rd_computed_vars(
55+
#' count = "number of points in bin.",
56+
#' density = "density of points in bin, scaled to integrate to 1.",
57+
#' ncount = "count, scaled to maximum of 1.",
58+
#' ndensity = "density, scaled to a maximum of 1."
59+
#' )
60+
stat_bin_2d <- make_constructor(StatBin2d, geom = "tile")
61+
62+
#' @export
63+
#' @rdname geom_bin_2d
64+
#' @usage NULL
65+
stat_bin2d <- stat_bin_2d
66+
9267
dual_param <- function(x, default = list(x = NULL, y = NULL)) {
9368
if (is.null(x)) {
9469
default

R/stat-binhex.R

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,9 @@
1-
#' @export
2-
#' @rdname geom_hex
3-
#' @inheritParams stat_bin_2d
4-
#' @eval rd_computed_vars(
5-
#' count = "number of points in bin.",
6-
#' density = "density of points in bin, scaled to integrate to 1.",
7-
#' ncount = "count, scaled to maximum of 1.",
8-
#' ndensity = "density, scaled to maximum of 1."
9-
#' )
10-
stat_bin_hex <- function(mapping = NULL, data = NULL,
11-
geom = "hex", position = "identity",
12-
...,
13-
bins = 30,
14-
binwidth = NULL,
15-
na.rm = FALSE,
16-
show.legend = NA,
17-
inherit.aes = TRUE) {
18-
layer(
19-
data = data,
20-
mapping = mapping,
21-
stat = StatBinhex,
22-
geom = geom,
23-
position = position,
24-
show.legend = show.legend,
25-
inherit.aes = inherit.aes,
26-
params = list2(
27-
bins = bins,
28-
binwidth = binwidth,
29-
na.rm = na.rm,
30-
...
31-
)
32-
)
33-
}
34-
35-
#' @export
36-
#' @rdname geom_hex
37-
#' @usage NULL
38-
stat_binhex <- stat_bin_hex
39-
401
#' @rdname ggplot2-ggproto
412
#' @format NULL
423
#' @usage NULL
434
#' @export
44-
StatBinhex <- ggproto("StatBinhex", Stat,
5+
StatBinhex <- ggproto(
6+
"StatBinhex", Stat,
457
default_aes = aes(weight = 1, fill = after_stat(count)),
468

479
required_aes = c("x", "y"),
@@ -66,3 +28,18 @@ StatBinhex <- ggproto("StatBinhex", Stat,
6628
dropped_aes = "weight"
6729
)
6830

31+
#' @export
32+
#' @rdname geom_hex
33+
#' @inheritParams stat_bin_2d
34+
#' @eval rd_computed_vars(
35+
#' count = "number of points in bin.",
36+
#' density = "density of points in bin, scaled to integrate to 1.",
37+
#' ncount = "count, scaled to maximum of 1.",
38+
#' ndensity = "density, scaled to maximum of 1."
39+
#' )
40+
stat_bin_hex <- make_constructor(StatBinhex, geom = "hex")
41+
42+
#' @export
43+
#' @rdname geom_hex
44+
#' @usage NULL
45+
stat_binhex <- stat_bin_hex

0 commit comments

Comments
 (0)