ggplot histogram with % and percentage *labels*

前端 未结 2 827
半阙折子戏
半阙折子戏 2020-12-21 08:14

I want to use ggplot to make a histogram using percentages. I found this answer that gets me part of the way there.

However, I also want to place a

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 08:18

    You'll want to just set the y values for your labels as well (and also make sure you're using the same bins are you are for the bars)

    library(scales)
    p <- ggplot(mtcars, aes(x = hp)) +  
            geom_bar(aes(y = (..count..)/sum(..count..)), binwidth = 25) + 
            scale_y_continuous(labels = percent_format()) #version 3.0.9
            ##scale_y_continuous(labels = percent) #version 3.1.0
    p <- p + stat_bin(aes(y=(..count..)/sum(..count..), 
        label=round((..count..)/sum(..count..),2)), 
        geom="text", size=4, binwidth = 25, vjust=-1.5)
    plot(p)
    

提交回复
热议问题