2

I am having an issue with creating a bar graph of proportions.

This is my current code:

ggplot(data = d,
       aes(fill = version)) +
  theme(legend.position = 'bottom') +
  geom_bar(aes(x = cregion,
               y = ..count../sum(..count..)),
           position = 'dodge') +
  scale_y_continuous(labels = percent) + 
  coord_flip()

Which produces this: derp-plot

This scales the bars so that the total sum is 100%, however. What I want is for the salmon bars to sum to 100%, and also for the aqua bars to sum to 100%. In other words, I am interested in the proportions within the scaling. Is there a way to do this with ggplot besides just going into my data and mucking around with my variables?

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Spencer
  • 860
  • 8
  • 16

1 Answers1

0

As of March 2017, with ggplot2 2.2.1 I think the best solution is explained in Hadley Wickham's R for data science book:

ggplot(data = d,aes(x=cregion)) + 
stat_count(aes(y=..prop.., group=version, fill = version, position = "dodge"))+
theme(legend.position = "bottom")+
scale_y_continuous(labels = percent) + 
coord_flip()

stat_count computes two variables: count is used by default, but you can choose to use prop which shows proportions.

Find the original answer at Show % instead of counts in charts of categorical variables