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

删除百分比轴-R中的小数

  •  0
  • Chris  · 技术社区  · 6 年前

    我有一个图,需要从百分位数中删除小数。数据已四舍五入(两位小数),数据示例:

    > head(df$X)
    [1] 0.05 0.28 0.08 0.19 0.33 
    

    然后我用作图 scale_x_continuous() 要将其转换为百分比格式,但如下图所示,它们仍然有一个小数,我需要那些不带小数的数字。

    Output

    这是图形的代码:

    ggplot(df, aes(x=df$X, y=DF$Y)) + 
        geom_point(
            colour="red",
            size=5
            ) + 
      geom_text(label=df$Label, vjust=-2, family="sans", fontface="bold") +
      labs(x="X", y="Y") + xlim(-1,1) + ylim(-1,1) + scale_x_continuous(labels=scales::percent) + scale_y_continuous(labels=scales::percent)
    

    我找到的最接近的答案是 this one 但我不需要粘贴“%”符号,因为我已经有了它。

    1 回复  |  直到 6 年前
        1
  •  5
  •   dave-edison    6 年前

    scales::percent 有一个 accuracy 可以修改的参数。这应该给你想要的:

    ggplot(df, aes(x, y)) + 
      geom_point(
        colour="red",
        size=5
      ) + 
      labs(x="X", y="Y") + 
      xlim(-1,1) + 
      ylim(-1,1) + 
      scale_x_continuous(labels = function(x) scales::percent(x, accuracy = 1)) + 
      scale_y_continuous(labels = function(x) scales::percent(x, accuracy = 1))