How to round percentage to 2 decimal places in ggplot2

前端 未结 4 1233
庸人自扰
庸人自扰 2020-12-20 17:30

My code produces this graph:

Code:

ggplot(areas, aes(x = RETAILER, y = Difference), label=sprintf(\"%0.2f\", round(areas$Difference, digits          


        
4条回答
  •  借酒劲吻你
    2020-12-20 18:03

    I use the percentfunction from formattable within geom_text. So in your case I would do like so:

    geom_text(aes(label = formattable::percent(Difference)),
              vjust = ifelse(Difference >= 0, -1.5, 1.5))
    

    you can define digits within percent, but the default is 2, which is what you're looking for. The % symbols remains, like you desire. Plus, you can do stuff with it as it is still numeric underneath and not character like other solutions out there.

提交回复
热议问题