Search code examples
rggplot2normalizationbin

Get relative count in ggplot combined with geom_bin2d, and proper use of scale_fill_gradientn


I am plotting 2d plot with my df composed of var_X and var_Y using ggplot2 combined with geom_bin2d and scale_fill_gradientn, but I have two issues to solve out.

(i) I need to get relative count for z (also in color bar) from 0 to 1, probably by normalizing the count to its maximum.

(ii) For the moment with absolute count, the max count appears to be ~1600 as indicated by the colorbar (colorbar ranged from ~0 to ~1600), but the plot shows blue to green (or yellow) colors only, not red.

Here is my code.

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
p<-ggplot(data.frame(df$var_X,df$var_Y), aes(df$var_X,df$var_Y)) + 
geom_bin2d()+ scale_x_continuous(trans=log_trans(), breaks=c(100,150, 200, 250))+
ylim(c(0, 200))+
scale_fill_gradientn(colours = jet.colors(7))
p

## or using ##
scale_fill_gradientn(colours = c("darkblue","lightblue", "green","yellow","red"))

I'd like to post my sample data, but do not know how to do it. Please let me know if you need.

Any suggestion would be very welcomed.


Solution

  • (i) It's not clear exactly how you would like to normalize. One way would be with 'geom_bin2d(aes(fill = ..density..))' as in the following.

    library(tidyverse)
    library(scales)
    df <- data.frame(var_X = runif(1000,0,1), var_Y = runif(1000,0,200))
    
    jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
    p<-ggplot(df, aes(df$var_X,df$var_Y)) + 
      geom_bin2d(aes(fill = ..density..)) +
      scale_x_continuous(trans=log_trans(), breaks=c(100,150, 200, 250)) +
      ylim(c(0, 200)) +
      scale_fill_gradientn(colours = jet.colors(7))
    p
    

    If you want to normalize so that the maximum bin value is 1 then something like this would need to work 'geom_bin2d(aes(fill = ..ndensity..))'. This doesn't seem to be possible in ggplot2 v3.0.0, but it is discussed here: https://github.com/tidyverse/ggplot2/issues/2679