How can I change the Y-axis figures into percentages in a barplot?

前端 未结 4 1572
清歌不尽
清歌不尽 2020-11-28 21:50

How can we change y axis to percent like the figure? I can change y axis range but I can\'t make it to percent. \"enter

4条回答
  •  伪装坚强ぢ
    2020-11-28 21:58

    Borrowed from @Deena above, that function modification for labels is more versatile than you might have thought. For example, I had a ggplot where the denominator of counted variables was 140. I used her example thus:

    scale_y_continuous(labels = function(x) paste0(round(x/140*100,1), "%"), breaks = seq(0, 140, 35))
    

    This allowed me to get my percentages on the 140 denominator, and then break the scale at 25% increments rather than the weird numbers it defaulted to. The key here is that the scale breaks are still set by the original count, not by your percentages. Therefore the breaks must be from zero to the denominator value, with the third argument in "breaks" being the denominator divided by however many label breaks you want (e.g. 140 * 0.25 = 35).

提交回复
热议问题