代码之家  ›  专栏  ›  技术社区  ›  Joe

如何防止scales::percent添加小数

  •  4
  • Joe  · 技术社区  · 6 年前

    这是几天前开始的 scales::percent 会在标签中添加一个小数位,而且我似乎不能禁用这个小数位来显示y轴上的整数值。 enter image description here

    library(dplyr)
    library(ggplot2)
    
    mtcars %>% 
      count(cyl) %>% 
      mutate(prop = n / sum(n)) %>% 
      ggplot(aes(x = cyl, y = prop)) + 
      geom_point() + 
      scale_y_continuous(labels = scales::percent)
    
    1 回复  |  直到 6 年前
        1
  •  20
  •   Community CDub    4 年前

    也许不能直接回答你的问题,但我用了 scales::percent_format 以及它的 accuracy 类似设置中的参数(“要舍入的数字”)。

    mtcars %>% 
        count(cyl) %>% 
        mutate(prop = n / sum(n)) %>% 
        ggplot(aes(x = cyl, y = prop)) + 
        geom_point() + 
        scale_y_continuous(labels = scales::percent_format(accuracy = 5L))
    

    enter image description here


    percent scales 1.0.0 . 看到了吗 NEWS here .

        2
  •  7
  •   landrower    4 年前

    scales::label_percent(accuracy = 1L) 将四舍五入为整数, scales::label_percent(accuracy = 0.1L)