0

Related to this question, I would like to produce an overlapping histogram where each group of data is scaled by dividing by the sum of that group's counts.

Borrowing the example and kohske's answer from the linked question, I have tried to use y = stat(count / sum(count)) combined with group = yy to scale the histograms, but this divides by the sum(count) for the whole dataset, not each group individually.

dat <- data.frame(xx = c(runif(100,20,50),runif(100,40,80),runif(30,0,30)),yy = rep(letters[1:3],c(100,100,30)))
ggplot(dat, aes(x=xx, y = stat(count / sum(count)), fill=yy, group=yy)) + 
    geom_histogram(alpha=0.2, position="identity")

overlapping histogram plot not normalising by group

How can I scale by the yy groups, and not for the whole dataset? I would like a plot where each bar represents the percentage of that group in the xx range to compare the shape of the distributions in a, b and c rather that the absolute count numbers.

lapsel
  • 75
  • 5

0 Answers0