Search code examples
ggplot2histogramfrequency

Multiple Relative frequency histogram in R, ggplot


I'm trying to plot multiple histograms of relative frequencies in R. ggplot


Solution

  • Below are some basic example with the build-in iris dataset. The relative part is obtained by multiplying the density with the binwidth.

    library(ggplot2)
    
    ggplot(iris, aes(Sepal.Length, fill = Species)) +
      geom_histogram(aes(y = after_stat(density * width)),
                     position = "identity", alpha = 0.5)
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    ggplot(iris, aes(Sepal.Length)) +
      geom_histogram(aes(y = after_stat(density * width))) +
      facet_wrap(~ Species)
    #> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
    

    Created on 2022-03-07 by the reprex package (v2.0.1)