Skip to content

Commit 800af57

Browse files
Document computed variable width and add a test (#4130)
* Document computed variable `width` and add a test * Add a NEWS
1 parent 325aef2 commit 800af57

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
`guides(<scale> = "none")` or `scale_*(guide = "none")` instead
1313
(@yutannihilation, #4094).
1414

15+
* `stat_bin()`'s computed variable `width` is now documented (#3522).
16+
1517
# ggplot2 3.3.2
1618
This is a small release focusing on fixing regressions introduced in 3.3.1.
1719

R/stat-bin.r

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#' \item{density}{density of points in bin, scaled to integrate to 1}
3434
#' \item{ncount}{count, scaled to maximum of 1}
3535
#' \item{ndensity}{density, scaled to maximum of 1}
36+
#' \item{width}{widths of bins}
3637
#' }
3738
#'
3839
#' @seealso [stat_count()], which counts the number of cases at each x

man/geom_histogram.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-stat-bin.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ test_that("geom_histogram() can be drawn over a 0-width range (#3043)", {
9292
expect_equal(out$xmax, 1.05)
9393
})
9494

95+
test_that("stat_bin() provides width (#3522)", {
96+
binwidth <- 1.03
97+
df <- data_frame(x = 1:10)
98+
p <- ggplot(df) +
99+
stat_bin(
100+
aes(
101+
x,
102+
xmin = after_stat(x - width / 2),
103+
xmax = after_stat(x + width / 2),
104+
ymin = after_stat(0),
105+
ymax = after_stat(count)
106+
),
107+
geom = "rect",
108+
binwidth = binwidth
109+
)
110+
out <- layer_data(p)
111+
112+
expect_equal(nrow(out), 10)
113+
# (x + width / 2) - (x - width / 2) = width
114+
expect_equal(out$xmax - out$xmin, rep(binwidth, 10))
115+
})
116+
95117
# Underlying binning algorithm --------------------------------------------
96118

97119
comp_bin <- function(df, ...) {

0 commit comments

Comments
 (0)