My dataset has three variables: longitude (x), latitude (y), and a continuous variable that for these purposes I'm calling z.
I am trying to use the stat_binhex function in the hexbin library to visualize the sum of z, rather than the count of (x,y) points, by hex bin. I know that there is a straightforward workaround for small sample sizes if I coerce z to a factor, but obviously this is neither feasible nor desirable with a large, continuous z.
library(ggplot2)
library(hexbin)
x <- runif(1000, -125, -65)
y <- runif(1000, 25, 50)
z <- runif(1000, 1, 30000000)
test <- data.frame(x=x, y=y, z=z)
p <- ggplot(test, aes(x=x, y=y, fill=z))
p <- p + stat_binhex(binwidth=c(.5,.5))
p
> Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0
I think the solution is in the manual to ggplot2. The function you may want is [stat_summary_hex][1]:
library(ggplot2)
library(hexbin)
x <- runif(1000, -125, -65)
y <- runif(1000, 25, 50)
z <- runif(1000, 1, 30000000)
test <- data.frame(x=x,
y=y,
z=z)
p <- ggplot(data = test,
aes(x = x,
y = y,
z = z)) +
stat_summary_hex(fun = function(x) sum(x))
print(p)
You'll end up with something like this:

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