Question

I'm plotting customer churn using the ggplot2 stat_summary_hex function.

stat_summary_hex(aes(x = Lon, y = Lat, z = Churn),
   bins=100, colour = NA, geom = "hex", fun = function(x) sum(x))

ScaleFill <- scale_fill_gradient2(low = "blue", high = "orange", na.value = NA)

The stat_summary_hex is plotted over a basemap from get_map so I would like to set the alpha scale such that summary values close to 0 would have an alpha of 0. However, it looks like stat_summary_hex does not acknowledge an alpha aesthetic.

Does someone have an example of stat_summary_hex with alpha mapping?

Was it helpful?

Solution

I found a work around by setting drop = TRUE and then changing the stat_summary_hex function to return NA when the result was less than my threshold:

stat_summary_hex(aes(x = Lon, y = Lat, z = Churn),
  bins=100, colour = NA, geom = "hex", drop = TRUE,
  fun = function(x) if(abs(sum(x)) > 5) {sum(x)} else {NA})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top