How to better create stacked bar graphs with multiple variables from ggplot2?

后端 未结 5 886
野趣味
野趣味 2020-12-15 11:24

I often have to make stacked barplots to compare variables, and because I do all my stats in R, I prefer to do all my graphics in R with ggplot2. I would like to learn how t

5条回答
  •  Happy的楠姐
    2020-12-15 12:01

    About percentages insted of ..count.. , try:

    ggplot(mtcars, aes(factor(cyl), prop.table(..count..) * 100)) + geom_bar()
    

    but since it's not a good idea to shove a function into the aes(), you can write custom function to create percentages out of ..count.. , round it to n decimals etc.

    You labeled this post with plyr, but I don't see any plyr in action here, and I bet that one ddply() can do the job. Online plyr documentation should suffice.

提交回复
热议问题