Skip to content

Commit 20aa3ed

Browse files
authored
Remove last bin if it lies outside of range (#4433)
1 parent a941cfb commit 20aa3ed

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

NEWS.md

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

3+
* Fix a bug in `stat_summary_bin()` where one more than the requested number of
4+
bins would be created (@thomasp85, #3824)
5+
36
* Fix a bug in `geom_abline()` that resulted in `intercept` not being subjected
47
to the transformation of the y scale (@thomasp85, #3741)
58

R/stat-bin2d.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ bin2d_breaks <- function(scale, breaks = NULL, origin = NULL, binwidth = NULL,
132132
if (!(is.numeric(origin) && length(origin) == 1)) abort("`origin` must be a numeric scalar")
133133

134134
breaks <- seq(origin, range[2] + binwidth, binwidth)
135+
136+
# Check if the last bin lies fully outside the range
137+
if (length(breaks) > 1 && breaks[length(breaks) - 1] >= range[2]) {
138+
breaks <- breaks[-length(breaks)]
139+
}
140+
135141
adjust_breaks(breaks, right)
136142
}
137143

0 commit comments

Comments
 (0)