diff --git a/NEWS.md b/NEWS.md index 3e92547b2a..53379376fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ ### Bug fixes +* Fixed bug where `stat_bin(boundary)` was ignored (#6682). * Fixed regression where `draw_key_rect()` stopped using `fill` colours (@mitchelloharawild, #6609). * Fixed regression where `scale_{x,y}_*()` threw an error when an expression diff --git a/R/bin.R b/R/bin.R index 1318a180e0..81a49aebec 100644 --- a/R/bin.R +++ b/R/bin.R @@ -105,7 +105,12 @@ bin_breaks_bins <- function(x_range, bins = 30, center = NULL, } else { width <- (x_range[2] - x_range[1]) / (bins - 1) if (is.null(center)) { - boundary <- boundary %||% x_range[1] - width / 2 + boundary <- boundary %||% (x_range[1] - width / 2) + } + # If `x_range` coincides with boundary we should + # use exact `bins` instead of `bins - 1` to prevent misalignments. + if (!is.null(boundary) && any(x_range %% width == boundary %% width)) { + width <- (x_range[2] - x_range[1]) / bins } }