Skip to content

Conversation

@teunbrand
Copy link
Collaborator

This PR aims to fix #6727.

It introduces the new radius aesthetic for geom_hex(), which let's us declare a relative size of the hexagon.

devtools::load_all("~/packages/ggplot2/")
ggplot(faithful, aes(waiting, eruptions)) +
  geom_hex(
    aes(radius = after_stat(ncount)), 
    fill = "black"
  )

Created on 2025-11-05 with reprex v2.1.1

I decided that this radius aesthetic it doesn't need a legend representation, because neither have width or height which are similarly position-related non-position aesthetics.

Draw method is faster now.

library(ggplot2)
p <- ggplot(diamonds, aes(carat, price)) +
  geom_hex(binwidth = c(0.05, 100))
b <- ggplot_build(p)
bench::mark(get_layer_grob(b), min_iterations = 100) # CRAN version
#> # A tibble: 1 × 6
#>   expression             min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>        <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 get_layer_grob(b)   5.36ms   5.82ms      169.    5.06MB     27.6

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
p <- ggplot(diamonds, aes(carat, price)) +
  geom_hex(binwidth = c(0.05, 100))
b <- ggplot_build(p)
bench::mark(get_layer_grob(b), min_iterations = 100) # This PR
#> # A tibble: 1 × 6
#>   expression             min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>        <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 get_layer_grob(b)   1.72ms   1.83ms      463.    4.23MB     90.2

Created on 2025-11-05 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: geom_hex() should support variable hex sizes

1 participant