Skip to content

Commit

Permalink
Set width as aesthetic in geom_col()/geom_bar(). (#5807)
Browse files Browse the repository at this point in the history
* replace hack with aesthetic

* remove `width` as parameter

* regenerate docs

* add news bullet
  • Loading branch information
teunbrand committed May 20, 2024
1 parent be3ae5a commit 9c4ac34
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 25 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
therefore stacking is not necessary (#5788).
* A new `ggplot_build()` S3 method for <ggplot_built> classes was added, which
returns input unaltered (@teunbrand, #5800).
* `width` is implemented as aesthetic instead of parameter in `geom_col()` and
`geom_bar()` (#3142).

# ggplot2 3.5.1

Expand Down
17 changes: 2 additions & 15 deletions R/geom-bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
#' columns to the left/right of axis breaks. Note that this argument may have
#' unintended behaviour when used with alternative positions, e.g.
#' `position_dodge()`.
#' @param width Bar width. By default, set to 90% of the [resolution()] of the
#' data.
#' @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.
Expand Down Expand Up @@ -98,7 +96,6 @@ geom_bar <- function(mapping = NULL, data = NULL,
stat = "count", position = "stack",
...,
just = 0.5,
width = NULL,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
Expand All @@ -113,7 +110,6 @@ geom_bar <- function(mapping = NULL, data = NULL,
inherit.aes = inherit.aes,
params = list2(
just = just,
width = width,
na.rm = na.rm,
orientation = orientation,
...
Expand All @@ -134,6 +130,8 @@ GeomBar <- ggproto("GeomBar", GeomRect,
# 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 = NULL),

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params)
params
Expand All @@ -158,16 +156,5 @@ GeomBar <- ggproto("GeomBar", GeomRect,
flip_data(data, params$flipped_aes)
},

draw_panel = function(self, data, panel_params, coord, lineend = "butt",
linejoin = "mitre", width = NULL, flipped_aes = FALSE) {
# Hack to ensure that width is detected as a parameter
ggproto_parent(GeomRect, self)$draw_panel(
data,
panel_params,
coord,
lineend = lineend,
linejoin = linejoin
)
},
rename_size = TRUE
)
2 changes: 0 additions & 2 deletions R/geom-col.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ geom_col <- function(mapping = NULL, data = NULL,
position = "stack",
...,
just = 0.5,
width = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE) {
Expand All @@ -19,7 +18,6 @@ geom_col <- function(mapping = NULL, data = NULL,
inherit.aes = inherit.aes,
params = list2(
just = just,
width = width,
na.rm = na.rm,
...
)
Expand Down
2 changes: 0 additions & 2 deletions R/stat-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
stat_count <- function(mapping = NULL, data = NULL,
geom = "bar", position = "stack",
...,
width = NULL,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
Expand All @@ -22,7 +21,6 @@ stat_count <- function(mapping = NULL, data = NULL,
params <- list2(
na.rm = na.rm,
orientation = orientation,
width = width,
...
)

Expand Down
8 changes: 2 additions & 6 deletions man/geom_bar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c4ac34

Please sign in to comment.