Skip to content

Commit 32a4a76

Browse files
committed
adopt boilerplate where possible
1 parent 5f067c1 commit 32a4a76

36 files changed

+942
-1493
lines changed

DESCRIPTION

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Collate:
9696
'geom-.R'
9797
'annotation-custom.R'
9898
'annotation-logticks.R'
99+
'scale-type.R'
100+
'layer.R'
101+
'boilerplates.R'
99102
'geom-polygon.R'
100103
'geom-map.R'
101104
'annotation-map.R'
@@ -108,9 +111,6 @@ Collate:
108111
'backports.R'
109112
'bench.R'
110113
'bin.R'
111-
'scale-type.R'
112-
'layer.R'
113-
'boilerplates.R'
114114
'coord-.R'
115115
'coord-cartesian-.R'
116116
'coord-fixed.R'
@@ -137,12 +137,14 @@ Collate:
137137
'geom-abline.R'
138138
'geom-rect.R'
139139
'geom-bar.R'
140+
'geom-tile.R'
140141
'geom-bin2d.R'
141142
'geom-blank.R'
142143
'geom-boxplot.R'
143144
'geom-col.R'
144145
'geom-path.R'
145146
'geom-contour.R'
147+
'geom-point.R'
146148
'geom-count.R'
147149
'geom-crossbar.R'
148150
'geom-segment.R'
@@ -162,15 +164,13 @@ Collate:
162164
'geom-jitter.R'
163165
'geom-label.R'
164166
'geom-linerange.R'
165-
'geom-point.R'
166167
'geom-pointrange.R'
167168
'geom-quantile.R'
168169
'geom-rug.R'
169170
'geom-sf.R'
170171
'geom-smooth.R'
171172
'geom-spoke.R'
172173
'geom-text.R'
173-
'geom-tile.R'
174174
'geom-violin.R'
175175
'geom-vline.R'
176176
'ggplot2-package.R'

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ S3method(as.data.frame,mapped_discrete)
1616
S3method(as.list,ggproto)
1717
S3method(autolayer,default)
1818
S3method(autoplot,default)
19+
S3method(boilerplate,Geom)
1920
S3method(c,mapped_discrete)
2021
S3method(drawDetails,zeroGrob)
2122
S3method(element_grob,element_blank)
@@ -297,6 +298,7 @@ export(autolayer)
297298
export(autoplot)
298299
export(benchplot)
299300
export(binned_scale)
301+
export(boilerplate)
300302
export(borders)
301303
export(calc_element)
302304
export(check_device)

R/geom-bar.R

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
#' @rdname ggplot2-ggproto
2+
#' @format NULL
3+
#' @usage NULL
4+
#' @export
5+
#' @include geom-rect.R
6+
GeomBar <- ggproto("GeomBar", GeomRect,
7+
required_aes = c("x", "y"),
8+
9+
# These aes columns are created by setup_data(). They need to be listed here so
10+
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
11+
# limits, not just those for which x and y are outside the limits
12+
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),
13+
14+
default_aes = aes(!!!GeomRect$default_aes, width = NULL),
15+
16+
setup_params = function(data, params) {
17+
params$flipped_aes <- has_flipped_aes(data, params)
18+
params
19+
},
20+
21+
extra_params = c("just", "na.rm", "orientation"),
22+
23+
setup_data = function(data, params) {
24+
data$flipped_aes <- params$flipped_aes
25+
data <- flip_data(data, params$flipped_aes)
26+
data$width <- data$width %||%
27+
params$width %||% (min(vapply(
28+
split(data$x, data$PANEL, drop = TRUE),
29+
resolution, numeric(1), zero = FALSE
30+
)) * 0.9)
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 = TRUE
41+
)
42+
143
#' Bar charts
244
#'
345
#' There are two types of bar charts: `geom_bar()` and `geom_col()`.
@@ -92,69 +134,7 @@
92134
#' ggplot(df, aes(x, y)) + geom_col(just = 0.5)
93135
#' # Columns begin on the first day of the month
94136
#' 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 ggplot2-ggproto
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 = NULL),
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(data, params) {
143-
data$flipped_aes <- params$flipped_aes
144-
data <- flip_data(data, params$flipped_aes)
145-
data$width <- data$width %||%
146-
params$width %||% (min(vapply(
147-
split(data$x, data$PANEL, drop = TRUE),
148-
resolution, numeric(1), zero = FALSE
149-
)) * 0.9)
150-
data$just <- params$just %||% 0.5
151-
data <- transform(data,
152-
ymin = pmin(y, 0), ymax = pmax(y, 0),
153-
xmin = x - width * just, xmax = x + width * (1 - just),
154-
width = NULL, just = NULL
155-
)
156-
flip_data(data, params$flipped_aes)
157-
},
158-
159-
rename_size = TRUE
137+
geom_bar <- boilerplate(
138+
GeomBar, stat = "count", position = "stack",
139+
just = 0.5, orientation = NA
160140
)

R/geom-bin2d.R

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#' @include geom-tile.R
2+
NULL
3+
14
#' Heatmap of 2d bin counts
25
#'
36
#' Divides the plane into rectangles, counts the number of cases in
@@ -26,27 +29,7 @@
2629
#'
2730
#' # Or by specifying the width of the bins
2831
#' d + geom_bin_2d(binwidth = c(0.1, 0.1))
29-
geom_bin_2d <- function(mapping = NULL, data = NULL,
30-
stat = "bin2d", position = "identity",
31-
...,
32-
na.rm = FALSE,
33-
show.legend = NA,
34-
inherit.aes = TRUE) {
35-
36-
layer(
37-
data = data,
38-
mapping = mapping,
39-
stat = stat,
40-
geom = GeomTile,
41-
position = position,
42-
show.legend = show.legend,
43-
inherit.aes = inherit.aes,
44-
params = list2(
45-
na.rm = na.rm,
46-
...
47-
)
48-
)
49-
}
32+
geom_bin_2d <- boilerplate(GeomTile, stat = "bin2d")
5033

5134
#' @export
5235
#' @rdname geom_bin_2d

R/geom-col.R

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
#' @export
22
#' @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-
}
3+
geom_col <- boilerplate(GeomBar, position = "stack", just = 0.5)
264

275
#' @rdname ggplot2-ggproto
286
#' @format NULL

R/geom-contour.R

Lines changed: 31 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
#' @rdname ggplot2-ggproto
2+
#' @format NULL
3+
#' @usage NULL
4+
#' @export
5+
#' @include geom-path.R
6+
GeomContour <- ggproto(
7+
"GeomContour", GeomPath,
8+
default_aes = aes(
9+
weight = 1,
10+
colour = from_theme(accent),
11+
linewidth = from_theme(linewidth),
12+
linetype = from_theme(linetype),
13+
alpha = NA
14+
)
15+
)
16+
17+
#' @rdname ggplot2-ggproto
18+
#' @format NULL
19+
#' @usage NULL
20+
#' @export
21+
#' @include geom-polygon.R
22+
GeomContourFilled <- ggproto("GeomContourFilled", GeomPolygon)
23+
124
#' 2D contours of a 3D surface
225
#'
326
#' @description
@@ -56,87 +79,15 @@
5679
#' v + geom_raster(aes(fill = density)) +
5780
#' geom_contour(colour = "white")
5881
#' }
59-
geom_contour <- function(mapping = NULL, data = NULL,
60-
stat = "contour", position = "identity",
61-
...,
62-
bins = NULL,
63-
binwidth = NULL,
64-
breaks = NULL,
65-
lineend = "butt",
66-
linejoin = "round",
67-
linemitre = 10,
68-
na.rm = FALSE,
69-
show.legend = NA,
70-
inherit.aes = TRUE) {
71-
layer(
72-
data = data,
73-
mapping = mapping,
74-
stat = stat,
75-
geom = GeomContour,
76-
position = position,
77-
show.legend = show.legend,
78-
inherit.aes = inherit.aes,
79-
params = list2(
80-
bins = bins,
81-
binwidth = binwidth,
82-
breaks = breaks,
83-
lineend = lineend,
84-
linejoin = linejoin,
85-
linemitre = linemitre,
86-
na.rm = na.rm,
87-
...
88-
)
89-
)
90-
}
82+
geom_contour <- boilerplate(
83+
GeomContour, stat = "contour",
84+
bins = NULL, binwidth = NULL, breaks = NULL,
85+
lineend = "butt", linejoin = "round", linemitre = 10
86+
)
9187

9288
#' @rdname geom_contour
9389
#' @export
94-
geom_contour_filled <- function(mapping = NULL, data = NULL,
95-
stat = "contour_filled", position = "identity",
96-
...,
97-
bins = NULL,
98-
binwidth = NULL,
99-
breaks = NULL,
100-
na.rm = FALSE,
101-
show.legend = NA,
102-
inherit.aes = TRUE) {
103-
layer(
104-
data = data,
105-
mapping = mapping,
106-
stat = stat,
107-
geom = GeomContourFilled,
108-
position = position,
109-
show.legend = show.legend,
110-
inherit.aes = inherit.aes,
111-
params = list2(
112-
bins = bins,
113-
binwidth = binwidth,
114-
breaks = breaks,
115-
na.rm = na.rm,
116-
...
117-
)
118-
)
119-
}
120-
121-
#' @rdname ggplot2-ggproto
122-
#' @format NULL
123-
#' @usage NULL
124-
#' @export
125-
#' @include geom-path.R
126-
GeomContour <- ggproto("GeomContour", GeomPath,
127-
default_aes = aes(
128-
weight = 1,
129-
colour = from_theme(accent),
130-
linewidth = from_theme(linewidth),
131-
linetype = from_theme(linetype),
132-
alpha = NA
133-
)
90+
geom_contour_filled <- boilerplate(
91+
GeomContourFilled, stat = "contour_filled",
92+
bins = NULL, binwidth = NULL, breaks = NULL
13493
)
135-
136-
#' @rdname ggplot2-ggproto
137-
#' @format NULL
138-
#' @usage NULL
139-
#' @export
140-
#' @include geom-polygon.R
141-
GeomContourFilled <- ggproto("GeomContourFilled", GeomPolygon)
142-

0 commit comments

Comments
 (0)