Skip to content

Variable sizes for geom_hex()#6729

Merged
teunbrand merged 8 commits into
tidyverse:mainfrom
teunbrand:hex_size
Dec 3, 2025
Merged

Variable sizes for geom_hex()#6729
teunbrand merged 8 commits into
tidyverse:mainfrom
teunbrand:hex_size

Conversation

@teunbrand

Copy link
Copy Markdown
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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Need a news bullet

@teunbrand teunbrand merged commit 3d88c7a into tidyverse:main Dec 3, 2025
13 checks passed
@teunbrand teunbrand deleted the hex_size branch December 3, 2025 15:52
@HRodenhizer

Copy link
Copy Markdown

Hopefully this isn't user error, but I'm not seeing radius as an aesthetic to geom_hex() using ggplot2 v4.0.3. It looks like it was merged quite awhile ago, but I don't see it on the release page. Am I missing something?

Using the example plot above:

ggplot(faithful, aes(waiting, eruptions)) +
  geom_hex(
    aes(radius = after_stat(ncount)), 
    fill = "black"
  )

I get the following warning:

Warning message:
In geom_hex(aes(radius = after_stat(ncount)), fill = "black") :
  Ignoring unknown aesthetics: radius

@teunbrand

Copy link
Copy Markdown
Collaborator Author

The 4.0.3 release was a hotfix, meaning it just fixed an urgent error (and a small feature somebody explicitly asked us to include). The radius aesthetic for geom_hex() is not yet in the 'live' version. You can install the development version to play around with this though, using pak::pak("tidyverse/ggplot2").

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

3 participants