I believe that there are two problems. Firstly, mode is not the function you want (check the help - this is "Get or set the type or storage mode of the object"). Secondly, the parameter fun= , not fun.x= for stat_summary_hex .
It's nice to discuss mode features here . Recommended Feature:
Mode <- function(x) { ux <- unique(x) ux[which.max(tabulate(match(x, ux)))] }
Finally, you want to make sure that the filling of the hexagons is considered as a discrete value. You can change the fun function so that the return value is a character (as in the code below).
Here is an example of reproducibility:
library(ggplot2) library(hexbin) Mode <- function(x) { ux <- unique(x) ux[which.max(tabulate(match(x, ux)))] } clusters=data.frame(xpos=rnorm(1000),ypos=rnorm(1000),cluster=rep(1:9,length.out=100)) ggplot(clusters, aes(x=xpos, y=ypos, z=cluster)) + stat_summary_hex(fun=function(x){as.character(Mode(x))})
Hope this helps.
source share