Search code examples
rggplot2gaussian

How to rescale 4 gaussian in order to make them sum to 1?


I am producing this plot with ggplot2, here each Gaussian sum to 1 but I would like that all 4 together sum to 1 so that the density on y axis should be divided of 4. Anyone know how to do it? Here the code that I am using my "data" i a table with 1 column of distances and 1 column with the relative cluster.

p5 <- ggplot(data,aes(x=distances, fill=cluster)) + 
  geom_density(alpha=0.25,adjust=4)+ 
  scale_fill_manual(values = c("seagreen2","darkseagreen2","palegreen4","palegreen1"))+
  xlab("distance (nm)")+theme_bw()
p5

enter image description here


Solution

  • According to ?geom_density computed variables section, the count variable is the density * number of observations (for that group). Hence if we divide the count variable by their sum (total observations between all groups), we should get proportional densities.

    library(ggplot2)
    
    df <- rbind(
      iris[1:75, ], # 50 setosa + 25 vericolor
      iris[101:111, ] # 10 virginica
    )
    
    ggplot(df, aes(Sepal.Width, fill = Species)) +
      geom_density(aes(y = after_stat(count / sum(count))))
    

    Created on 2021-04-14 by the reprex package (v1.0.0)

    Note that if you want the divide the densities by the number of densities, you can use after_stat(density / sum(density))) (since every density sums to 1).