I would like to create a barplot like this:
library(ggplot2)
# Dodged bar charts
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge")
However, instead of counts, I want to have the percentage of observations falling into each 'clarity' category by cutting category ('fair', 'good', 'very good' ...).
With this ...
# Dodged bar charts
ggplot(diamonds, aes(clarity, fill=cut)) +
geom_bar(aes(y = (..count..)/sum(..count..)), position="dodge")
I get percentages on the y-axis, but these percentages ignore the cut-factor. I want that all the red bars sum up to 1, all the yellow bars sum up to 1 etc.
Is there an easy way to make that work without having to prepare the data manually?
Thanks!
P.S.: This is a follow-up to this stackoverflow question