Home > Software engineering >  Geom_Hex coloring by 3rd variable
Geom_Hex coloring by 3rd variable

Time:03-26

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)

CodePudding user response:

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

CodePudding user response:

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

  • Related