Skip to content

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

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

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ ggplot2 1.0.1
* Improvements to order of colours and legend display for continuous colour
scales extracted from colorbrewer palettes by `scale_*_distiller()` (@jiho, 1076)

* Restore functionality for use of `..density..` in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go at the top of NEWS.md (just underneath the version number heading)

`geom_hexbin()` (@mikebirdgeneau, #1688)

ggplot2 1.0.0
----------------------------------------------------------------

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)
})