I’d like to a find a way to keep regular hexagons (all sides have equal length) when resizing hexbin plots in ggplot2 without manually adjusting the binwidth parameter.
To illustrate:
d <- ggplot(diamonds, aes(carat, price))+
stat_binhex(colour="white")
try(ggsave(plot=d,filename=<some file>,height=6,width=8))
yields hexagons that at least look regular to the eye:
And
try(ggsave(plot=d,filename=<some other file>,height=6,width=12))
yields irregular hexagons:
The documentation describes the binwidth parameter (e.g. binwidth = c(1, 1000)) which specifies bin width. I’d like a function which, when given any plot size, returns the right binwidth settings to create regular hexagons.
Here’s the solution to adjust binwidth dynamically. I’ve included handling for portrait aspect ratios and explicitly stated axis limits.
For example this code:
Yields:

And when we change the width:
Or change the height:
We can also change the x and y limits: