Skip to content

Commit a7371db

Browse files
teunbrandyjunechoe
andauthored
Boilerplate functions (#6143)
* write boilerplate function * adopt boilerplate where possible * write out all non-standard arguments * automatically fill in parameters * incorporate small checks * document * accept visual snapshots * Update R/boilerplates.R Thanks June! Co-authored-by: June Choe <52832839+yjunechoe@users.noreply.github.com> * Adopt advice from June * docfix * Apply suggestions from code review Co-authored-by: June Choe <52832839+yjunechoe@users.noreply.github.com> * ensure `list2()` can be found * rename `boilerplate()` to `make_constructor()` * give body pretty braces * purge unused parameters in `stat_bin()` * add Stat method for `make_constructor()` * use the `make_constructor()` for Stats * revert 4dd78b5 for `layer_sf()` stats * mechanism to hide internal variables * document * make GeomCol parent of `geom_col()` again for error message purposes * rebalance omissions * include example of `checks` argument * Hide origin/right arguments that are deprecated * avoid arbitrary whitespace changes * apply boilerplate to `stat_connect()` * Also boilerplate `stat_manual()` * boilerplate `geom_text()` * remove deprecated args * add news bullet --------- Co-authored-by: June Choe <52832839+yjunechoe@users.noreply.github.com>
1 parent 4f9b9b4 commit a7371db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2279
-3065
lines changed

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Collate:
9898
'geom-.R'
9999
'annotation-custom.R'
100100
'annotation-logticks.R'
101+
'scale-type.R'
102+
'layer.R'
103+
'make-constructor.R'
101104
'geom-polygon.R'
102105
'geom-map.R'
103106
'annotation-map.R'
@@ -142,6 +145,7 @@ Collate:
142145
'geom-col.R'
143146
'geom-path.R'
144147
'geom-contour.R'
148+
'geom-point.R'
145149
'geom-count.R'
146150
'geom-crossbar.R'
147151
'geom-segment.R'
@@ -160,7 +164,6 @@ Collate:
160164
'geom-jitter.R'
161165
'geom-label.R'
162166
'geom-linerange.R'
163-
'geom-point.R'
164167
'geom-pointrange.R'
165168
'geom-quantile.R'
166169
'geom-rug.R'
@@ -186,7 +189,6 @@ Collate:
186189
'guide-colorbar.R'
187190
'guide-colorsteps.R'
188191
'guide-custom.R'
189-
'layer.R'
190192
'guide-none.R'
191193
'guide-old.R'
192194
'guides-.R'
@@ -236,7 +238,6 @@ Collate:
236238
'scale-shape.R'
237239
'scale-size.R'
238240
'scale-steps.R'
239-
'scale-type.R'
240241
'scale-view.R'
241242
'scale-viridis.R'
242243
'scales-.R'

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ S3method(limits,character)
9595
S3method(limits,factor)
9696
S3method(limits,numeric)
9797
S3method(makeContext,dotstackGrob)
98+
S3method(make_constructor,Geom)
99+
S3method(make_constructor,Stat)
98100
S3method(merge_element,default)
99101
S3method(merge_element,element)
100102
S3method(merge_element,element_blank)
@@ -507,6 +509,7 @@ export(layer_grob)
507509
export(layer_scales)
508510
export(layer_sf)
509511
export(lims)
512+
export(make_constructor)
510513
export(map_data)
511514
export(margin)
512515
export(margin_auto)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* New `make_constructor()` function that builds a standard constructor for
4+
Geom and Stat classes (@teunbrand, #6142).
35
* In continuous scales, when `breaks` is a function and `n.breaks` is set, the
46
`n.breaks` will be passed to the `breaks` function. Previously, `n.breaks`
57
only applied to the default break calculation (@teunbrand, #5972)

R/coord-transform.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ CoordTransform <- ggproto(
199199
)
200200

201201
# TODO: deprecate this some time in the future
202-
#' @rdname ggplot2-ggproto
202+
#' @rdname Coord
203203
#' @format NULL
204204
#' @usage NULL
205205
#' @export

R/geom-bar.R

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
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+
143
#' Bar charts
244
#'
345
#' There are two types of bar charts: `geom_bar()` and `geom_col()`.
@@ -48,6 +90,8 @@
4890
#' @param geom,stat Override the default connection between `geom_bar()` and
4991
#' `stat_count()`. For more information about overriding these connections,
5092
#' 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).
5195
#' @examples
5296
#' # geom_bar is designed to make it easy to create bar charts that show
5397
#' # counts (or sums of weights)
@@ -92,68 +136,7 @@
92136
#' ggplot(df, aes(x, y)) + geom_col(just = 0.5)
93137
#' # Columns begin on the first day of the month
94138
#' 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
159142
)

R/geom-bin2d.R

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#' @include geom-tile.R
22
NULL
33

4+
#' @rdname Geom
5+
#' @format NULL
6+
#' @usage NULL
7+
#' @export
8+
GeomBin2d <- ggproto("GeomBin2d", GeomTile)
9+
410
#' Heatmap of 2d bin counts
511
#'
612
#' Divides the plane into rectangles, counts the number of cases in
@@ -17,6 +23,8 @@ NULL
1723
#' `geom_bin_2d()` and `stat_bin_2d()`. For more information about overriding
1824
#' these connections, see how the [stat][layer_stats] and [geom][layer_geoms]
1925
#' arguments work.
26+
#' @param lineend Line end style (round, butt, square).
27+
#' @param linejoin Line join style (round, mitre, bevel).
2028
#' @seealso [stat_bin_hex()] for hexagonal binning
2129
#' @examples
2230
#' d <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)
@@ -29,35 +37,9 @@ NULL
2937
#'
3038
#' # Or by specifying the width of the bins
3139
#' d + geom_bin_2d(binwidth = c(0.1, 0.1))
32-
geom_bin_2d <- function(mapping = NULL, data = NULL,
33-
stat = "bin2d", position = "identity",
34-
...,
35-
na.rm = FALSE,
36-
show.legend = NA,
37-
inherit.aes = TRUE) {
38-
39-
layer(
40-
data = data,
41-
mapping = mapping,
42-
stat = stat,
43-
geom = GeomBin2d,
44-
position = position,
45-
show.legend = show.legend,
46-
inherit.aes = inherit.aes,
47-
params = list2(
48-
na.rm = na.rm,
49-
...
50-
)
51-
)
52-
}
40+
geom_bin_2d <- make_constructor(GeomBin2d, stat = "bin2d")
5341

5442
#' @export
5543
#' @rdname geom_bin_2d
5644
#' @usage NULL
5745
geom_bin2d <- geom_bin_2d
58-
59-
#' @rdname Geom
60-
#' @format NULL
61-
#' @usage NULL
62-
#' @export
63-
GeomBin2d <- ggproto("GeomBin2d", GeomTile)

R/geom-col.R

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
1-
#' @export
2-
#' @rdname geom_bar
3-
geom_col <- function(mapping = NULL, data = NULL,
4-
position = "stack",
5-
...,
6-
just = 0.5,
7-
na.rm = FALSE,
8-
show.legend = NA,
9-
inherit.aes = TRUE) {
10-
11-
layer(
12-
data = data,
13-
mapping = mapping,
14-
stat = "identity",
15-
geom = GeomCol,
16-
position = position,
17-
show.legend = show.legend,
18-
inherit.aes = inherit.aes,
19-
params = list2(
20-
just = just,
21-
na.rm = na.rm,
22-
...
23-
)
24-
)
25-
}
26-
271
#' @rdname Geom
282
#' @format NULL
293
#' @usage NULL
304
#' @export
315
#' @include geom-rect.R
326
# TODO: deprecate this
337
GeomCol <- ggproto("GeomCol", GeomBar)
8+
9+
#' @export
10+
#' @rdname geom_bar
11+
geom_col <- make_constructor(GeomCol, position = "stack", just = 0.5)

0 commit comments

Comments
 (0)