Skip to content

geom_hexbin(): hexes appear stretched? #6461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kevinushey opened this issue May 16, 2025 · 2 comments
Closed

geom_hexbin(): hexes appear stretched? #6461

kevinushey opened this issue May 16, 2025 · 2 comments

Comments

@kevinushey
Copy link
Contributor

Using this code, from the examples:

d <- ggplot(diamonds, aes(carat, price))
d + geom_hex()

It seems like the plotted hexagons are now "squished" improperly, according to the aspect ratio of the current device. For example:

Image Image

Has it always been this way and I'm just misremembering? Or has something changed in how hexagonal bins are drawn?

Compare this with plots generated by e.g.

library(hexbin)
mixdata <-
  data.frame(x = c(rnorm(5000),rnorm(5000,4,1.5)),
             y = c(rnorm(5000),rnorm(5000,2,3)),
             a = gl(2, 5000))
hexbinplot(y ~ x, aspect = 2, data = mixdata)
Image Image
@kevinushey
Copy link
Contributor Author

> sessioninfo::session_info()
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.2 (2024-10-31)
 os       macOS Sequoia 15.4.1
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Los_Angeles
 date     2025-05-16
 rstudio  2025.08.0-hourly+75 Sandyland Bluebonnet (desktop)
 pandoc   3.6.4 @ /opt/homebrew/bin/pandoc
 quarto   1.7.29 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/quarto

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package      * version date (UTC) lib source
 cli            3.6.5   2025-04-23 [1] CRAN (R 4.4.1)
 dplyr          1.1.4   2023-11-17 [1] CRAN (R 4.4.0)
 farver         2.1.2   2024-05-13 [1] CRAN (R 4.4.1)
 generics       0.1.4   2025-05-09 [1] CRAN (R 4.4.1)
 ggplot2      * 3.5.2   2025-04-09 [1] RSPM
 glue           1.8.0   2024-09-30 [1] CRAN (R 4.4.1)
 gtable         0.3.6   2024-10-25 [1] CRAN (R 4.4.1)
 lifecycle      1.0.4   2023-11-07 [1] CRAN (R 4.4.1)
 magrittr       2.0.3   2022-03-30 [1] CRAN (R 4.4.1)
 pillar         1.10.2  2025-04-05 [1] CRAN (R 4.4.1)
 pkgconfig      2.0.3   2019-09-22 [1] CRAN (R 4.4.1)
 R6             2.6.1   2025-02-15 [1] CRAN (R 4.4.1)
 RColorBrewer   1.1-3   2022-04-03 [1] CRAN (R 4.4.0)
 rlang          1.1.6   2025-04-11 [1] RSPM (R 4.4.2)
 rstudioapi     0.17.1  2024-10-22 [1] CRAN (R 4.4.1)
 scales         1.4.0   2025-04-24 [1] CRAN (R 4.4.1)
 sessioninfo    1.2.3   2025-02-05 [1] CRAN (R 4.4.1)
 tibble         3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
 tidyselect     1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
 vctrs          0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
 withr          3.0.2   2024-10-28 [1] CRAN (R 4.4.1)

 [1] /Users/kevin/Library/R/arm64/4.4/library
 [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
 * ── Packages attached to the search path.

@thomasp85
Copy link
Member

This has always been the case. The binning happens before ggplot2 knows anything about the aspect ratio of the plot and is not recalculated on resizing. So you'll need to use a coord_fixed() to ensure the hexagon is regular

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

No branches or pull requests

2 participants