diff --git a/NEWS.md b/NEWS.md index 1b4dc36ba0..76bf5100a3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # ggplot2 (development version) +* Fixed a regression in `geom_hex()` where aesthetics were replicated across + bins (@thomasp85, #5037 and #5044) * Fixed spurious warning when `weight` aesthetic was used in `stat_smooth()` (@teunbrand based on @clauswilke's suggestion, #5053). * The `lwd` alias now correctly replaced by `linewidth` instead of `size` 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 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 46ec1143c5..e63e159438 100644 --- a/tests/testthat/test-geom-hex.R +++ b/tests/testthat/test-geom-hex.R @@ -17,8 +17,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", {