Well, as a starting point, here are some sample data. Each of them is random, one is shifted by (2.2).
df1 <- data.frame( x = rnorm(1000) , y = rnorm(1000) ) df2 <- data.frame( x = rnorm(1000, 2) , y = rnorm(1000, 2) )
To ensure the identity of the bins, it is best to create one hexbin
object. To do this, I use dplyr
bind_rows
to track which data.frame the data was received from (this would be even easier if you had a single data.frame with a grouping variable).
bothDF <- bind_rows(A = df1, B = df2, .id = "df") bothHex <- hexbin(x = bothDF$x , y = bothDF$y , IDs = TRUE )
Then we use a combination of hexbin
and dplyr
to count the occurrences of each of them in each cell. First, apply through the bunkers by building a table (you need to use factor
to make sure that all levels are shown, not needed if your column is already a factor). He then simplifies it and builds a data.frame, which is then controlled using mutate
to calculate the difference in the counts, and then bound to a table that gives x and y values ββfor each of the identifiers.
counts <- hexTapply(bothHex, factor(bothDF$df), table) %>% simplify2array %>% t %>% data.frame() %>% mutate(id = as.numeric(row.names(.)) , diff = A - B) %>% left_join(data.frame(id = bothHex@cell , hcell2xy(bothHex)))
head(counts)
gives:
AB id diff xy 1 1 0 7 1 -1.3794467 -3.687014 2 1 0 71 1 -0.8149939 -3.178209 3 1 0 79 1 1.4428172 -3.178209 4 1 0 99 1 -1.5205599 -2.923806 5 2 0 105 2 0.1727985 -2.923806 6 1 0 107 1 0.7372513 -2.923806
Finally, we use ggplot2
to build the resulting data, since it offers more control (and the ability to more easily use a different variable than the fill counter) than hexbin
.
counts %>% ggplot(aes(x = x, y = y , fill = diff)) + geom_hex(stat = "identity") + coord_equal() + scale_fill_gradient2()
From there, it's easy to inspect axes, colors, etc.