Display frequency instead of count with geom_bar() in ggplot

前端 未结 2 1281
别跟我提以往
别跟我提以往 2020-12-10 10:33

On this page, they give the following example

library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat=\"bin\")

Ins

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 11:12

    Here's the solution which can be found in related question:

    pp <- ggplot(data=tips, aes(x=day)) + 
          geom_bar(aes(y = (..count..)/sum(..count..)))
    

    If you would like to label frequencies as percentage, add this (see here):

    library(scales)
    pp + scale_y_continuous(labels = percent)
    

提交回复
热议问题