rggplot2colorshistogram

Density histogram with different fill colours


I'm looking to create a density histogram (i.e. histogram with density, not counts, on y-axis), with certain portions of the histogram coloured according to categories. Here's a simple example:

library(ggplot2)

#Data
d <- diamonds
priceBreaks <- c(0,5000,10000,20000) #Create discrete breaks in price
d$priceCat <- cut(diamonds$price,breaks = priceBreaks,
                  labels = c('low','med','high'))

#Create figure
ggplot(d)+
  geom_histogram(aes(x=price,fill=priceCat),
                 breaks=seq(0,20000,by=1000)) #Align column breaks with price breaks

enter image description here

However, when I try to rescale the y-axis to density rather than count using after_stat(), this happens:

ggplot(d)+
  geom_histogram(aes(x=price,y=after_stat(density),fill=priceCat),
                 breaks=seq(0,20000,by=1000))

enter image description here

This is splitting up the density calculation across the different categories. How do I get it to use overall density? The stage function is likely the way this is done, but it's unclear to me how it's specified.


Solution

  • I think the tricky part here is that you want fill to apply to different groups of bars, but you want the density to be calculated with everything in one group.

    One workaround could be to calculate the density semi-manually and to adjust it for the bin width using after_stat(width) to get the right density:

    ggplot(d)+
      geom_histogram(
        aes(x=price,
            y=..count.. / sum(..count..) / after_stat(width),
            fill=priceCat),
        breaks=seq(0,20000,by=1000))
    

    enter image description here