Show percent % instead of counts in charts of categorical variables

前端 未结 8 2399
梦如初夏
梦如初夏 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:26

    If you want percentage labels but actual Ns on the y axis, try this:

        library(scales)
    perbar=function(xx){
          q=ggplot(data=data.frame(xx),aes(x=xx))+
          geom_bar(aes(y = (..count..)),fill="orange")
           q=q+    geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") 
          q
        }
        perbar(mtcars$disp)
    

提交回复
热议问题