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.
Your choices are to set coord_fixed
with an appropriate ratio so that the plot will not stretch over the size of the graphics device
In this case 5/17000
would appear reasonable
d <- ggplot(diamonds, aes(carat, price))+
stat_binhex(colour="white") + coord_fixed(ratio = 5/17000)
The other option is to create the binwidths and ratio of the coordinate dimensions with the ratio of device dimensions in mind.
Unless the coordinate ratio is fixed (as per my first example), you can't expect to stretch the same plot into a window that is 1.5 times wider, without the plot looking stretched.
so if you are stretching the width by a factor of 1.5, then decrease the binwidth in the x
dimension by a factor 1.5
d <- ggplot(diamonds, aes(carat, price))+
stat_binhex(colour="white",bin.widths = c((5/45),17000/30 ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With