Instead of trying to get two different fill scales on the same chart, you can change the colors of the lower values ββafter plotting. The main idea is to have two graphs with different filling scales, and then copy over certain parts from one section to another.
# Base plot p <- ggplot() + stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) + stat_bin_hex(data=B, aes(x=X, y=Y), bins=10) # Produce two plots with different fill colours p1 <- p + scale_fill_continuous(low="red4", high="#ED1A3A") p2 <- p + scale_fill_continuous(low="darkgreen", high="lightgreen") # Get fill colours for second plot and overwrite the corresponding # values in the first plot g1 <- ggplot_build(p1) g2 <- ggplot_build(p2) g1$data[[1]][,"fill"] <- g2$data[[1]][,"fill"] # You can draw this now but there is only one legend grid.draw(ggplot_gtable(g1))
To have two legends, you can join the legends of two graphs together
# Bind the legends from the two plots together g1 <- ggplot_gtable(g1) g2 <- ggplot_gtable(g2) g1$grobs[[grep("guide", g1$layout$name )]] <- rbind(g1$grobs[[grep("guide", g1$layout$name )]], g2$grobs[[grep("guide", g2$layout$name )]] ) grid.newpage() grid.draw(g1)
Submission (from set.seed(10) to data generation)

source share