Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to include count & density to fix the use of geom_hexbin #1688

Closed
wants to merge 13 commits into from
Closed
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)

* `x` and `y` scales are now symmetric regarding the list of
aesthetics they accept: `xmin_final`, `xmax_final`, `xlower`,
`xmiddle` and `xupper` are now valid `x` aesthetics.
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)
})