Show percent % instead of counts in charts of categorical variables

前端 未结 8 2589
梦如初夏
梦如初夏 2020-11-22 06:06

I\'m plotting a categorical variable and instead of showing the counts for each category value.

I\'m looking for a way to get ggplot to display the perc

8条回答
  •  天命终不由人
    2020-11-22 06:19

    this modified code should work

    p = ggplot(mydataf, aes(x = foo)) + 
        geom_bar(aes(y = (..count..)/sum(..count..))) + 
        scale_y_continuous(formatter = 'percent')
    

    if your data has NAs and you dont want them to be included in the plot, pass na.omit(mydataf) as the argument to ggplot.

    hope this helps.

提交回复
热议问题