Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `geom_hex()` will now use the binwidth from `stat_bin_hex()` if present,
instead of deriving it (@thomasp85, #4580)

* Setting `stroke` to `NA` in `geom_point()` will no longer impair the sizing of
the points (@thomasp85, #4624)

Expand Down
26 changes: 22 additions & 4 deletions R/geom-hex.r
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,31 @@ geom_hex <- function(mapping = NULL, data = NULL,
GeomHex <- ggproto("GeomHex", Geom,
draw_group = function(data, panel_params, coord, lineend = "butt",
linejoin = "mitre", linemitre = 10) {
if (empty(data)) {
return(zeroGrob())
}
if (!inherits(coord, "CoordCartesian")) {
abort("geom_hex() only works with Cartesian coordinates")
}
# Extract binwidth and height from data if possible
if (!is.null(data$width)) {
data$xend <- data$x + data$width
}
if (!is.null(data$height)) {
data$yend <- data$y + data$height
}

coords <- coord$transform(data, panel_params)

binwidth <- c(NA, NA)
if (!is.null(data$width)) {
binwidth[1] <- coords$xend[1] - coords$x[1]
}
if (!is.null(data$height)) {
binwidth[2] <- coords$yend[1] - coords$y[1]
}
ggname("geom_hex", hexGrob(
coords$x, coords$y,
coords$x, coords$y, binwidth,
gp = gpar(
col = coords$colour,
fill = alpha(coords$fill, coords$alpha),
Expand Down Expand Up @@ -97,11 +115,11 @@ GeomHex <- ggproto("GeomHex", Geom,
# @param size vector of hex sizes
# @param gp graphical parameters
# @keyword internal
hexGrob <- function(x, y, size = rep(1, length(x)), gp = gpar()) {
hexGrob <- function(x, y, binwidth, size = rep(1, length(x)), gp = gpar()) {
if (length(y) != length(x)) abort("`x` and `y` must have the same length")

dx <- resolution(x, FALSE)
dy <- resolution(y, FALSE) / sqrt(3) / 2 * 1.15
dx <- if (is.na(binwidth[1])) resolution(x, FALSE) else binwidth[1]/2
dy <- if (is.na(binwidth[2])) resolution(y, FALSE) / sqrt(3) / 2 * 1.15 else binwidth[2]/ sqrt(3) / 2

hexC <- hexbin::hexcoords(dx, dy, n = 1)

Expand Down
2 changes: 2 additions & 0 deletions R/hexbin.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ hexBinSummarise <- function(x, y, z, binwidth, fun = mean, fun.args = list(), dr
# Convert to data frame
out <- new_data_frame(hexbin::hcell2xy(hb))
out$value <- as.vector(value)
out$width <- binwidth[1]
out$height <- binwidth[2]

if (drop) out <- stats::na.omit(out)
out
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/testthat/test-geom-hex.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ test_that("size and linetype are applied", {
expect_equal(gpar$lwd, c(4, 4) * .pt, tolerance = 1e-7)
expect_equal(gpar$lty, c(2, 2), tolerance = 1e-7)
})

test_that("bin size are picked up from stat", {
expect_doppelganger("single hex bin with width and height of 0.1",
ggplot(data.frame(x = 0, y = 0)) +
geom_hex(aes(x = x, y = y), binwidth = c(0.1, 0.1)) +
coord_cartesian(xlim = c(-1, 1), ylim = c(-1, 1))
)
})