Hexbin in R ggplot - hexagons get bigger if data is too sparse

679 views Asked by At

I'm generating a series of hexbin plots for use in an animated GIF, and there are occasional frames that have a low density of data. The plots seem to create giant, misshapen hexagons.

Here is an example that works as expected:

library(ggplot2)
set.seed(23)
x <- rnorm(10000)
y <- rnorm(10000)
temp <- data.frame(x, y)
ggplot(temp) + stat_binhex(aes(x=x,y=y), bins=30) + scale_fill_gradientn(colours=c("white","blue"))

However, limiting it to 3 data points gives abnormal bins:

set.seed(23)
x2 <- rnorm(3)
y2 <- rnorm(3)
temp2 <- data.frame(x2, y2)
ggplot(temp2) + stat_binhex(aes(x=x2,y=y2), bins=30) + scale_fill_gradientn(colours=c("white","blue"))

I'd like to keep the same hexagon sizes (bins=30) and just have it color the 3 hexagons that contain data.

0

There are 0 answers