|
| 1 | +#' @rdname Geom |
| 2 | +#' @format NULL |
| 3 | +#' @usage NULL |
| 4 | +#' @export |
| 5 | +#' @include geom-rect.R |
| 6 | +GeomBar <- ggproto( |
| 7 | + "GeomBar", GeomRect, |
| 8 | + required_aes = c("x", "y"), |
| 9 | + |
| 10 | + # These aes columns are created by setup_data(). They need to be listed here so |
| 11 | + # that GeomRect$handle_na() properly removes any bars that fall outside the defined |
| 12 | + # limits, not just those for which x and y are outside the limits |
| 13 | + non_missing_aes = c("xmin", "xmax", "ymin", "ymax"), |
| 14 | + |
| 15 | + default_aes = aes(!!!GeomRect$default_aes, width = 0.9), |
| 16 | + |
| 17 | + setup_params = function(data, params) { |
| 18 | + params$flipped_aes <- has_flipped_aes(data, params) |
| 19 | + params |
| 20 | + }, |
| 21 | + |
| 22 | + extra_params = c("just", "na.rm", "orientation"), |
| 23 | + |
| 24 | + setup_data = function(self, data, params) { |
| 25 | + data$flipped_aes <- params$flipped_aes |
| 26 | + data <- flip_data(data, params$flipped_aes) |
| 27 | + data <- compute_data_size( |
| 28 | + data, size = params$width, |
| 29 | + default = self$default_aes$width, zero = FALSE |
| 30 | + ) |
| 31 | + data$just <- params$just %||% 0.5 |
| 32 | + data <- transform(data, |
| 33 | + ymin = pmin(y, 0), ymax = pmax(y, 0), |
| 34 | + xmin = x - width * just, xmax = x + width * (1 - just), |
| 35 | + width = NULL, just = NULL |
| 36 | + ) |
| 37 | + flip_data(data, params$flipped_aes) |
| 38 | + }, |
| 39 | + |
| 40 | + rename_size = FALSE |
| 41 | +) |
| 42 | + |
1 | 43 | #' Bar charts |
2 | 44 | #' |
3 | 45 | #' There are two types of bar charts: `geom_bar()` and `geom_col()`. |
|
48 | 90 | #' @param geom,stat Override the default connection between `geom_bar()` and |
49 | 91 | #' `stat_count()`. For more information about overriding these connections, |
50 | 92 | #' see how the [stat][layer_stats] and [geom][layer_geoms] arguments work. |
| 93 | +#' @param lineend Line end style (round, butt, square). |
| 94 | +#' @param linejoin Line join style (round, mitre, bevel). |
51 | 95 | #' @examples |
52 | 96 | #' # geom_bar is designed to make it easy to create bar charts that show |
53 | 97 | #' # counts (or sums of weights) |
|
92 | 136 | #' ggplot(df, aes(x, y)) + geom_col(just = 0.5) |
93 | 137 | #' # Columns begin on the first day of the month |
94 | 138 | #' ggplot(df, aes(x, y)) + geom_col(just = 1) |
95 | | -geom_bar <- function(mapping = NULL, data = NULL, |
96 | | - stat = "count", position = "stack", |
97 | | - ..., |
98 | | - just = 0.5, |
99 | | - na.rm = FALSE, |
100 | | - orientation = NA, |
101 | | - show.legend = NA, |
102 | | - inherit.aes = TRUE) { |
103 | | - layer( |
104 | | - data = data, |
105 | | - mapping = mapping, |
106 | | - stat = stat, |
107 | | - geom = GeomBar, |
108 | | - position = position, |
109 | | - show.legend = show.legend, |
110 | | - inherit.aes = inherit.aes, |
111 | | - params = list2( |
112 | | - just = just, |
113 | | - na.rm = na.rm, |
114 | | - orientation = orientation, |
115 | | - ... |
116 | | - ) |
117 | | - ) |
118 | | -} |
119 | | - |
120 | | -#' @rdname Geom |
121 | | -#' @format NULL |
122 | | -#' @usage NULL |
123 | | -#' @export |
124 | | -#' @include geom-rect.R |
125 | | -GeomBar <- ggproto("GeomBar", GeomRect, |
126 | | - required_aes = c("x", "y"), |
127 | | - |
128 | | - # These aes columns are created by setup_data(). They need to be listed here so |
129 | | - # that GeomRect$handle_na() properly removes any bars that fall outside the defined |
130 | | - # limits, not just those for which x and y are outside the limits |
131 | | - non_missing_aes = c("xmin", "xmax", "ymin", "ymax"), |
132 | | - |
133 | | - default_aes = aes(!!!GeomRect$default_aes, width = 0.9), |
134 | | - |
135 | | - setup_params = function(data, params) { |
136 | | - params$flipped_aes <- has_flipped_aes(data, params) |
137 | | - params |
138 | | - }, |
139 | | - |
140 | | - extra_params = c("just", "na.rm", "orientation"), |
141 | | - |
142 | | - setup_data = function(self, data, params) { |
143 | | - data$flipped_aes <- params$flipped_aes |
144 | | - data <- flip_data(data, params$flipped_aes) |
145 | | - data <- compute_data_size( |
146 | | - data, size = params$width, |
147 | | - default = self$default_aes$width, zero = FALSE |
148 | | - ) |
149 | | - data$just <- params$just %||% 0.5 |
150 | | - data <- transform(data, |
151 | | - ymin = pmin(y, 0), ymax = pmax(y, 0), |
152 | | - xmin = x - width * just, xmax = x + width * (1 - just), |
153 | | - width = NULL, just = NULL |
154 | | - ) |
155 | | - flip_data(data, params$flipped_aes) |
156 | | - }, |
157 | | - |
158 | | - rename_size = FALSE |
| 139 | +geom_bar <- make_constructor( |
| 140 | + GeomBar, |
| 141 | + stat = "count", position = "stack", just = 0.5 |
159 | 142 | ) |
0 commit comments