4

I'm trying to create a normal hexbin plot but instead of coloring the plot by count, the default, I would like to have it colored by the average value of a third variable. In my particular case, I cannot use the stat_summary_hex function.

library(ggplot2)
library(hexbin)

x <- rnorm(1e4, 0, 5)                                                     
y <- rnorm(1e4, 0, 10)  
z <- rnorm(1e4, 20, 1)

data.frame(x, y, z) %>%                                                   
  ggplot(mapping = aes(x = x, y = y, z = z)) +
  geom_hex(bins = 20)
Justin
  • 153
  • 2
  • 10
  • why is it not possible to use the `stat_summary_hex` function? – Quinten Mar 25 '22 at 20:00
  • This plot will be interactive (ggiraph) and that package does not have an interactive version of the stat_summary_hex function – Justin Mar 25 '22 at 20:09

2 Answers2

5

You can use the following code:

library(ggplot2)
library(hexbin)
library(ggraph)

x <- rnorm(1e4, 0, 5)                                                     
y <- rnorm(1e4, 0, 10)  
z <- rnorm(1e4, 20, 1)

data.frame(x, y, z) %>%                                                                                            
  ggplot(aes(x, y, z=z)) + 
  stat_summary_hex(fun = function(x) mean(x), bins = 20) +
  scale_fill_viridis_c(option = "magma")

ggplotly()

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
3

This is tricky. You need to assign the fill value after the stat is computed. You can do this by precalculating the hexbins that the stat is going to produce, and using hexTapply to get the average of z in each cell.

hb <- hexbin(x, y, bins = 30)

data.frame(x, y, z) %>%                                                   
  ggplot(mapping = aes(x = x, y = y, weight = z)) +
  geom_hex(aes(fill = after_stat(hexTapply(hb, z, mean))), bins = 30) +
  scale_fill_viridis_c(option = "magma")

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thank you. When I try to implement this I get an error about needing cell IDs. I passed that through the hexbin function and then get a different error about differing number of rows – Justin Mar 25 '22 at 20:15
  • On another note, (and this probably warrants its own question post), is there a way to limit the plotting of hexbins that do not meet a certain count requirement? – Justin Mar 25 '22 at 21:02