Skip to content

Commit 9a1a203

Browse files
committed
Expand binwidth when needed
Fixes #1610
1 parent 6d84ac2 commit 9a1a203

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 2.1.0.9000
22

3+
* `stat_bin()` and `stat_summary_hex()` now accept length 1 `binwidth` (#1610)
4+
35
* `geom_histogram()` and `stat_bin()` understand the `breaks` parameter once
46
more. (#1665).
57

R/hexbin.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ hex_bounds <- function(x, binwidth) {
1313
}
1414

1515
hexBinSummarise <- function(x, y, z, binwidth, fun = mean, fun.args = list(), drop = TRUE) {
16+
if (length(binwidth) == 1) {
17+
binwidth <- rep(binwidth, 2)
18+
}
19+
1620
# Convert binwidths into bounds + nbins
1721
xbnds <- hex_bounds(x, binwidth[1])
1822
xbins <- diff(xbnds) / binwidth[1]

tests/testthat/test-stat-hex.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
context("stat-hex")
2+
3+
test_that("can use length 1 binwidth", {
4+
df <- data.frame(x = c(1, 1, 2), y = c(1, 1, 2))
5+
p <- ggplot(df, aes(x, y)) + stat_binhex(binwidth = 1)
6+
7+
expect_equal(nrow(layer_data(p)), 2)
8+
})

0 commit comments

Comments
 (0)