|
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 |
| - |
85 | 1 | #' @rdname ggplot2-ggproto
|
86 | 2 | #' @format NULL
|
87 | 3 | #' @usage NULL
|
88 | 4 | #' @export
|
89 |
| -StatBin <- ggproto("StatBin", Stat, |
| 5 | +StatBin <- ggproto( |
| 6 | + "StatBin", Stat, |
90 | 7 | setup_params = function(self, data, params) {
|
91 | 8 | params$flipped_aes <- has_flipped_aes(data, params, main_is_orthogonal = FALSE)
|
92 | 9 |
|
@@ -171,3 +88,53 @@ StatBin <- ggproto("StatBin", Stat,
|
171 | 88 | dropped_aes = "weight" # after statistical transformation, weights are no longer available
|
172 | 89 | )
|
173 | 90 |
|
| 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 | +) |
0 commit comments