Rounding % Labels on bar chart in ggplot2

前端 未结 1 1189
说谎
说谎 2021-01-08 01:29
q1 <- qplot(factor(Q1), data=survey, geom=\"histogram\", fill=factor(Q1), ylim=c(0,300))

options(digits=2)

q1 + geom_bar(colour=\"black\") + 
stat_bin(aes(label         


        
相关标签:
1条回答
  • 2021-01-08 01:48

    Actually you are very close to there.
    Here is a minimal example:

    df <- data.frame(x = factor(sample(5, 99, T)))
    ggplot(df, aes(x)) + 
      stat_bin(aes(label = paste(sprintf("%.02f", ..count../sum(..count..)*100), "%")), 
               geom="text")
    

    also, format, round, prettyNum, etc, is available.

    UPDATED:

    Thanks to @Tommy 's comment, here si a more simple form:

    ggplot(df, aes(x)) + 
     stat_bin(aes(label = sprintf("%.02f %%", ..count../sum(..count..)*100)),
         geom="text")
    
    0 讨论(0)
提交回复
热议问题