@ cryo111 answer has the most important ingredient - IDs = TRUE . After that, you just need to figure out what you want to do with Inf and how much you need to scale the relationship to get integers that will create quite a plot.
library(hexbin) library(data.table) set.seed(1) x = rnorm(10000) y = rnorm(10000) h = hexbin(x, y, IDs = TRUE) # put all the relevant data in a data.table dt = data.table(x, y, l = c(1,1,1,2), cID = h@cID ) # group by cID and calculate whatever statistic you like # in this case, ratio of 1 to 2's, # and then Inf are set to be equal to the largest ratio dt[, list(ratio = sum(l == 1)/sum(l == 2)), keyby = cID][, ratio := ifelse(ratio == Inf, max(ratio[is.finite(ratio)]), ratio)][, # scale up (I chose a scaling manually to get a prettier graph) # and convert to integer and change h as.integer(ratio*10)] -> h@count plot(h)

source share