2

I am trying to visualise the proportion of two factors (i.e the counts) w.r.t to a binning over a continuous variable. A small running example is the following.

df <- data.frame(s=as.factor(c("0","1","1","0","1","0","0")),
                 density=c(1.1,1.3,1.4,2.1,2.3,2.2,2.4),
                 n=as.factor(c("A","A","A","A","A","A","A")))

ggplot(df, aes(x = density, fill = s)) + 
  geom_histogram(position = "stack", bins = 2) + 
  facet_wrap("n", scales = "free")

resulting in the following plot:

Simple histogram with a fill by factor

Now what I would like is that, instead of absolute counts, I get percentages on the y axis, where 100% is the total number of records, and the the filling indicates how many percent are covered by factor level 0 and how many by factor level 1.

Jaap
  • 81,064
  • 34
  • 182
  • 193
matentzn
  • 337
  • 1
  • 10

0 Answers0