Skip to content

Boilerplate functions #6143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5f067c1
write boilerplate function
teunbrand Oct 16, 2024
32a4a76
adopt boilerplate where possible
teunbrand Oct 16, 2024
3eee99f
write out all non-standard arguments
teunbrand Oct 17, 2024
26f9f8f
automatically fill in parameters
teunbrand Oct 17, 2024
8c8f795
incorporate small checks
teunbrand Oct 17, 2024
fa9f6d2
document
teunbrand Oct 17, 2024
075ea86
accept visual snapshots
teunbrand Oct 17, 2024
ea57200
Update R/boilerplates.R
teunbrand Oct 18, 2024
6b44f33
Adopt advice from June
teunbrand Oct 18, 2024
6e71e1a
docfix
teunbrand Oct 18, 2024
d488de8
Apply suggestions from code review
teunbrand Oct 18, 2024
e6db72d
ensure `list2()` can be found
teunbrand Oct 21, 2024
8c33474
rename `boilerplate()` to `make_constructor()`
teunbrand Oct 22, 2024
c11c5ed
give body pretty braces
teunbrand Oct 22, 2024
44cf2dc
purge unused parameters in `stat_bin()`
teunbrand Oct 22, 2024
cd54bfb
add Stat method for `make_constructor()`
teunbrand Nov 28, 2024
4dd78b5
use the `make_constructor()` for Stats
teunbrand Nov 28, 2024
c7abdd7
revert 4dd78b5c for `layer_sf()` stats
teunbrand Nov 28, 2024
d1043b0
mechanism to hide internal variables
teunbrand Nov 28, 2024
a68e4bb
document
teunbrand Nov 28, 2024
6f452f4
resolve merge conflict
teunbrand Nov 28, 2024
6c95f24
make GeomCol parent of `geom_col()` again for error message purposes
teunbrand Nov 28, 2024
52e66f2
rebalance omissions
teunbrand Nov 28, 2024
295e36c
resolve merge conflict
teunbrand Feb 4, 2025
c7aa7fd
include example of `checks` argument
teunbrand Feb 4, 2025
23f4c71
Hide origin/right arguments that are deprecated
teunbrand Feb 4, 2025
453cf6b
avoid arbitrary whitespace changes
teunbrand Mar 25, 2025
9039e10
resolve merge conflict
teunbrand Mar 25, 2025
9ca4353
apply boilerplate to `stat_connect()`
teunbrand Mar 25, 2025
015cdc1
Also boilerplate `stat_manual()`
teunbrand Mar 25, 2025
c5f38c5
boilerplate `geom_text()`
teunbrand Mar 25, 2025
f7652fe
remove deprecated args
teunbrand Mar 25, 2025
7625aa2
resolve merge conflict
teunbrand May 6, 2025
5d79442
resolve merge conflict
teunbrand May 19, 2025
e0320f6
add news bullet
teunbrand May 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Collate:
'geom-.R'
'annotation-custom.R'
'annotation-logticks.R'
'scale-type.R'
'layer.R'
'make-constructor.R'
'geom-polygon.R'
'geom-map.R'
'annotation-map.R'
Expand Down Expand Up @@ -142,6 +145,7 @@ Collate:
'geom-col.R'
'geom-path.R'
'geom-contour.R'
'geom-point.R'
'geom-count.R'
'geom-crossbar.R'
'geom-segment.R'
Expand All @@ -160,7 +164,6 @@ Collate:
'geom-jitter.R'
'geom-label.R'
'geom-linerange.R'
'geom-point.R'
'geom-pointrange.R'
'geom-quantile.R'
'geom-rug.R'
Expand All @@ -186,7 +189,6 @@ Collate:
'guide-colorbar.R'
'guide-colorsteps.R'
'guide-custom.R'
'layer.R'
'guide-none.R'
'guide-old.R'
'guides-.R'
Expand Down Expand Up @@ -236,7 +238,6 @@ Collate:
'scale-shape.R'
'scale-size.R'
'scale-steps.R'
'scale-type.R'
'scale-view.R'
'scale-viridis.R'
'scales-.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ S3method(limits,character)
S3method(limits,factor)
S3method(limits,numeric)
S3method(makeContext,dotstackGrob)
S3method(make_constructor,Geom)
S3method(make_constructor,Stat)
S3method(merge_element,default)
S3method(merge_element,element)
S3method(merge_element,element_blank)
Expand Down Expand Up @@ -507,6 +509,7 @@ export(layer_grob)
export(layer_scales)
export(layer_sf)
export(lims)
export(make_constructor)
export(map_data)
export(margin)
export(margin_auto)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 (development version)

* New `make_constructor()` function that builds a standard constructor for
Geom and Stat classes (@teunbrand, #6142).
* In continuous scales, when `breaks` is a function and `n.breaks` is set, the
`n.breaks` will be passed to the `breaks` function. Previously, `n.breaks`
only applied to the default break calculation (@teunbrand, #5972)
Expand Down
2 changes: 1 addition & 1 deletion R/coord-transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ CoordTransform <- ggproto(
)

# TODO: deprecate this some time in the future
#' @rdname ggplot2-ggproto
#' @rdname Coord
#' @format NULL
#' @usage NULL
#' @export
Expand Down
111 changes: 47 additions & 64 deletions R/geom-bar.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
#' @rdname Geom
#' @format NULL
#' @usage NULL
#' @export
#' @include geom-rect.R
GeomBar <- ggproto(
"GeomBar", GeomRect,
required_aes = c("x", "y"),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),

default_aes = aes(!!!GeomRect$default_aes, width = 0.9),

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params)
params
},

extra_params = c("just", "na.rm", "orientation"),

setup_data = function(self, data, params) {
data$flipped_aes <- params$flipped_aes
data <- flip_data(data, params$flipped_aes)
data <- compute_data_size(
data, size = params$width,
default = self$default_aes$width, zero = FALSE
)
data$just <- params$just %||% 0.5
data <- transform(data,
ymin = pmin(y, 0), ymax = pmax(y, 0),
xmin = x - width * just, xmax = x + width * (1 - just),
width = NULL, just = NULL
)
flip_data(data, params$flipped_aes)
},

rename_size = FALSE
)

#' Bar charts
#'
#' There are two types of bar charts: `geom_bar()` and `geom_col()`.
Expand Down Expand Up @@ -48,6 +90,8 @@
#' @param geom,stat Override the default connection between `geom_bar()` and
#' `stat_count()`. For more information about overriding these connections,
#' see how the [stat][layer_stats] and [geom][layer_geoms] arguments work.
#' @param lineend Line end style (round, butt, square).
#' @param linejoin Line join style (round, mitre, bevel).
#' @examples
#' # geom_bar is designed to make it easy to create bar charts that show
#' # counts (or sums of weights)
Expand Down Expand Up @@ -92,68 +136,7 @@
#' ggplot(df, aes(x, y)) + geom_col(just = 0.5)
#' # Columns begin on the first day of the month
#' ggplot(df, aes(x, y)) + geom_col(just = 1)
geom_bar <- function(mapping = NULL, data = NULL,
stat = "count", position = "stack",
...,
just = 0.5,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomBar,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
just = just,
na.rm = na.rm,
orientation = orientation,
...
)
)
}

#' @rdname Geom
#' @format NULL
#' @usage NULL
#' @export
#' @include geom-rect.R
GeomBar <- ggproto("GeomBar", GeomRect,
required_aes = c("x", "y"),

# These aes columns are created by setup_data(). They need to be listed here so
# that GeomRect$handle_na() properly removes any bars that fall outside the defined
# limits, not just those for which x and y are outside the limits
non_missing_aes = c("xmin", "xmax", "ymin", "ymax"),

default_aes = aes(!!!GeomRect$default_aes, width = 0.9),

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params)
params
},

extra_params = c("just", "na.rm", "orientation"),

setup_data = function(self, data, params) {
data$flipped_aes <- params$flipped_aes
data <- flip_data(data, params$flipped_aes)
data <- compute_data_size(
data, size = params$width,
default = self$default_aes$width, zero = FALSE
)
data$just <- params$just %||% 0.5
data <- transform(data,
ymin = pmin(y, 0), ymax = pmax(y, 0),
xmin = x - width * just, xmax = x + width * (1 - just),
width = NULL, just = NULL
)
flip_data(data, params$flipped_aes)
},

rename_size = FALSE
geom_bar <- make_constructor(
GeomBar,
stat = "count", position = "stack", just = 0.5
)
36 changes: 9 additions & 27 deletions R/geom-bin2d.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#' @include geom-tile.R
NULL

#' @rdname Geom
#' @format NULL
#' @usage NULL
#' @export
GeomBin2d <- ggproto("GeomBin2d", GeomTile)

#' Heatmap of 2d bin counts
#'
#' Divides the plane into rectangles, counts the number of cases in
Expand All @@ -17,6 +23,8 @@ NULL
#' `geom_bin_2d()` and `stat_bin_2d()`. For more information about overriding
#' these connections, see how the [stat][layer_stats] and [geom][layer_geoms]
#' arguments work.
#' @param lineend Line end style (round, butt, square).
#' @param linejoin Line join style (round, mitre, bevel).
#' @seealso [stat_bin_hex()] for hexagonal binning
#' @examples
#' d <- ggplot(diamonds, aes(x, y)) + xlim(4, 10) + ylim(4, 10)
Expand All @@ -29,35 +37,9 @@ NULL
#'
#' # Or by specifying the width of the bins
#' d + geom_bin_2d(binwidth = c(0.1, 0.1))
geom_bin_2d <- function(mapping = NULL, data = NULL,
stat = "bin2d", position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {

layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomBin2d,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
na.rm = na.rm,
...
)
)
}
geom_bin_2d <- make_constructor(GeomBin2d, stat = "bin2d")

#' @export
#' @rdname geom_bin_2d
#' @usage NULL
geom_bin2d <- geom_bin_2d

#' @rdname Geom
#' @format NULL
#' @usage NULL
#' @export
GeomBin2d <- ggproto("GeomBin2d", GeomTile)
30 changes: 4 additions & 26 deletions R/geom-col.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
#' @export
#' @rdname geom_bar
geom_col <- function(mapping = NULL, data = NULL,
position = "stack",
...,
just = 0.5,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {

layer(
data = data,
mapping = mapping,
stat = "identity",
geom = GeomCol,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list2(
just = just,
na.rm = na.rm,
...
)
)
}

#' @rdname Geom
#' @format NULL
#' @usage NULL
#' @export
#' @include geom-rect.R
# TODO: deprecate this
GeomCol <- ggproto("GeomCol", GeomBar)

#' @export
#' @rdname geom_bar
geom_col <- make_constructor(GeomCol, position = "stack", just = 0.5)
Loading