Skip to content

Commit

Permalink
Allow geom_hexbin to use ..density..
Browse files Browse the repository at this point in the history
Fixes #1608. Closes #1688. By @mikebirdgeneau
  • Loading branch information
hadley committed Aug 5, 2016
1 parent 95f4346 commit 56e2dad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 2.1.0.9000

* Restore functionality for use of `..density..` in
`geom_hexbin()` (@mikebirdgeneau, #1688)

* `stat_smooth()` once again informs you about the method it has chosen.
It also correctly calculates the size of the largest group within facets.

Expand Down
5 changes: 4 additions & 1 deletion R/stat-binhex.r
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ StatBinhex <- ggproto("StatBinhex", Stat,

binwidth <- binwidth %||% hex_binwidth(bins, scales)
wt <- data$weight %||% rep(1L, nrow(data))
hexBinSummarise(data$x, data$y, wt, binwidth, sum)
out <- hexBinSummarise(data$x, data$y, wt, binwidth, sum)
out$density <- as.vector(out$value / sum(out$value, na.rm = TRUE))

out
}
)

12 changes: 12 additions & 0 deletions tests/testthat/test-geom-hex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("geom_hex")

test_that("density and value summaries available", {
df <- data.frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2))
base <- ggplot(df, aes(x, y)) +
geom_hex()

out <- layer_data(base)
expect_equal(nrow(out), 2)
expect_equal(out$density, c(0.75, 0.25), tolerance = 1e-7)
expect_equal(out$value, c(3, 1), tolerance = 1e-7)
})

0 comments on commit 56e2dad

Please sign in to comment.