Skip to content

Commit 56e2dad

Browse files
committed
Allow geom_hexbin to use ..density..
Fixes #1608. Closes #1688. By @mikebirdgeneau
1 parent 95f4346 commit 56e2dad

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

NEWS.md

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

3+
* Restore functionality for use of `..density..` in
4+
`geom_hexbin()` (@mikebirdgeneau, #1688)
5+
36
* `stat_smooth()` once again informs you about the method it has chosen.
47
It also correctly calculates the size of the largest group within facets.
58

R/stat-binhex.r

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ StatBinhex <- ggproto("StatBinhex", Stat,
4646

4747
binwidth <- binwidth %||% hex_binwidth(bins, scales)
4848
wt <- data$weight %||% rep(1L, nrow(data))
49-
hexBinSummarise(data$x, data$y, wt, binwidth, sum)
49+
out <- hexBinSummarise(data$x, data$y, wt, binwidth, sum)
50+
out$density <- as.vector(out$value / sum(out$value, na.rm = TRUE))
51+
52+
out
5053
}
5154
)
5255

tests/testthat/test-geom-hex.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
context("geom_hex")
2+
3+
test_that("density and value summaries available", {
4+
df <- data.frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2))
5+
base <- ggplot(df, aes(x, y)) +
6+
geom_hex()
7+
8+
out <- layer_data(base)
9+
expect_equal(nrow(out), 2)
10+
expect_equal(out$density, c(0.75, 0.25), tolerance = 1e-7)
11+
expect_equal(out$value, c(3, 1), tolerance = 1e-7)
12+
})

0 commit comments

Comments
 (0)