4

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.

user3641120
  • 75
  • 1
  • 4
  • It appears this is purely a matter of the autoscaled limits. Try setting the x- and y- axis limits to fixed values, perhaps `+/-5` for both plots, and see how that looks. – Carl Witthoft Dec 23 '14 at 15:09
  • I think adjusting x and y axis limit would be a good solution. If you want the size of hex in your first example, I think that you wanna have something like this. `scale_x_continuous(limits = c(-5, 17)) + scale_y_continuous(limits = c(-10, 10))`. If you add this to your second example, you may see a graphic you are looking for. – jazzurro Dec 23 '14 at 16:02
  • I have exactly the same problem here! My use case is that i combine hexbin with a facet wrap, and some facets simply has too sparse data (lots of NA). Because the facets share scales, i cannot reset limits. So i still end up with one facet where the hex bins had distorted size. – Ying Zhang May 15 '19 at 23:06

0 Answers0