r - Ggplot Heatmap - customized colors for customized count ranges -


i want make heatmap creates group of clarity & color combinations x axis , cut y axis. heatmap color based upon counts of clarity+color , intersection cut.

library(ggplot2) library(dplyr)   ## rename diamonds df  #  1.   generate count frequency of cut+clarity   #  2.   make heatmap of using following bins  #  3.    red <= 100 frequency           yellow  = between (100 , 500)           green   > 500 # place counts inside cell:  df = diamonds %>%  select( cut, clarity) %>%  group_by(cut,clarity)%>%  mutate(count  = n())  myplot = ggplot(df, aes(x = clarity, y=cut)) +  geom_bin2d( bins = c(100,500,50000), col='orange')  #  geom_text( aes(label = count),col='red')  myplot 

try this:

df$col <- cut(df$count,breaks = c(-inf,100,500,inf),right = true) df$color<-df$col levels(df$color) <- c("<=100","100<#<=500",">500")  ggplot(data =  df, aes(x = clarity, y = cut)) +    geom_tile(aes(fill = df$color), colour = "white") +   scale_fill_brewer("count",palette = "set1")+ geom_text(aes(label = count),col='yellow',cex=3) 

![enter image description here


Comments