From 85c168008347a6a9334d8ef55801a6c54c0d9182 Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Fri, 11 Nov 2022 09:58:39 +0100 Subject: [PATCH 1/2] avoid replication of aesthetics across hex bins --- NEWS.md | 3 +++ R/geom-hex.r | 16 ++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 582d9f2794..a552fd1588 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* Fixed a regression in `geom_hex()` where aesthetics were replicated across + bins (@thomasp85, #5037 and #5044) + # ggplot2 3.4.0 This is a minor release focusing on tightening up the internals and ironing out some inconsistencies in the API. The biggest change is the addition of the diff --git a/R/geom-hex.r b/R/geom-hex.r index 4a3942fa32..0e6af52745 100644 --- a/R/geom-hex.r +++ b/R/geom-hex.r @@ -80,19 +80,19 @@ GeomHex <- ggproto("GeomHex", Geom, n <- nrow(data) - data <- data[rep(seq_len(n), each = 6), ] - data$x <- rep.int(hexC$x, n) + data$x - data$y <- rep.int(hexC$y, n) + data$y + hexdata <- data[rep(seq_len(n), each = 6), c("x", "y")] + hexdata$x <- rep.int(hexC$x, n) + hexdata$x + hexdata$y <- rep.int(hexC$y, n) + hexdata$y - coords <- coord$transform(data, panel_params) + coords <- coord$transform(hexdata, panel_params) ggname("geom_hex", polygonGrob( coords$x, coords$y, gp = gpar( - col = coords$colour, - fill = alpha(coords$fill, coords$alpha), - lwd = coords$linewidth * .pt, - lty = coords$linetype, + col = data$colour, + fill = alpha(data$fill, data$alpha), + lwd = data$linewidth * .pt, + lty = data$linetype, lineend = lineend, linejoin = linejoin, linemitre = linemitre From 374eef75ce10781bac17617dfeeb87ad84db642f Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Fri, 11 Nov 2022 11:33:12 +0100 Subject: [PATCH 2/2] Update tests --- .../hex-bin-plot-in-polar-coordinates.svg | 172 +++++++++--------- .../hex-bin-plot-with-sqrt-transformed-y.svg | 172 +++++++++--------- tests/testthat/test-geom-hex.R | 4 +- 3 files changed, 174 insertions(+), 174 deletions(-) diff --git a/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg b/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg index 34bc427bd8..3ce3eb7f00 100644 --- a/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg +++ b/tests/testthat/_snaps/geom-hex/hex-bin-plot-in-polar-coordinates.svg @@ -37,115 +37,115 @@ - - - - - + + + + + - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + - - - - - - + + + + + + - - - + + + - + - + - - - - - - - + + + + + + + - - + + - - - - + + + + - - - - - + + + + + - - - + + + - - - - - - - - + + + + + + + + - + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + 2 3 diff --git a/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg b/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg index 82486696e1..193b3500c3 100644 --- a/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg +++ b/tests/testthat/_snaps/geom-hex/hex-bin-plot-with-sqrt-transformed-y.svg @@ -28,115 +28,115 @@ - - - - - + + + + + - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + - - - - - - + + + + + + - - - + + + - + - + - - - - - - - + + + + + + + - - + + - - - - + + + + - - - - - + + + + + - - - + + + - - - - - - - - + + + + + + + + - + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + diff --git a/tests/testthat/test-geom-hex.R b/tests/testthat/test-geom-hex.R index 08669fdb88..7e645dbdbc 100644 --- a/tests/testthat/test-geom-hex.R +++ b/tests/testthat/test-geom-hex.R @@ -15,8 +15,8 @@ test_that("size and linetype are applied", { geom_hex(color = "red", linewidth = 4, linetype = 2) gpar <- layer_grob(plot)[[1]]$children[[1]]$gp - expect_equal(gpar$lwd, rep(4, 12) * .pt, tolerance = 1e-7) - expect_equal(gpar$lty, rep(2, 12), tolerance = 1e-7) + expect_equal(gpar$lwd, rep(4, 2) * .pt, tolerance = 1e-7) + expect_equal(gpar$lty, rep(2, 2), tolerance = 1e-7) }) test_that("bin size are picked up from stat", {