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

删除小数y轴ggplot2

  •  2
  • KGB91  · 技术社区  · 7 年前

    df_Filtered

    Product Relative_Value
    Car     0.12651458
    Plane   0.08888552
    Tank    0.03546231
    Bike    0.06711630
    Train   0.06382191
    

    我想对GGplot2中的数据做一个条形图:

    ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
        scale_y_continuous(labels = scales::percent) +
        geom_bar(stat = "identity") +
        theme_bw() +
        theme(plot.background = element_rect(colour = "black", size = 1)) +
        theme(legend.position = "none") +
        theme(plot.title = element_text(hjust = 0.5))
        labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
        theme(panel.grid.major = element_blank())
    

    我如何去掉图表中y轴上的小数?所以它说 20 % 而不是 20.0 %

    1 回复  |  直到 7 年前
        1
  •  21
  •   www    6 年前

    使用 percent_format scales accuracy 到1。

    library(ggplot2)
    library(scales)
    
    ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
      scale_y_continuous(labels = percent_format(accuracy = 1)) +
      geom_bar(stat = "identity") +
      theme_bw() +
      theme(plot.background = element_rect(colour = "black", size = 1)) +
      theme(legend.position = "none") +
      theme(plot.title = element_text(hjust = 0.5)) +
    labs(x ="Product", y = "Percentage of total sell", title = "Japan 2010") +
      theme(panel.grid.major = element_blank()) 
    

    enter image description here

    数据

    df_Filtered <- read.table(text = "Product Relative_Value
    Car     0.12651458
                     Plane   0.08888552
                     Tank    0.03546231
                     Bike    0.06711630
                     Train   0.06382191",
                     header = TRUE, stringsAsFactors = FALSE)
    
        2
  •  1
  •   Sabbir Ahmed Hemo    6 年前

    scales::percent_format(accuracy = 2) breaks = c(0, 0.5, .10) . 所以,我必须创建手动函数 scale_y_continuous(breaks = c(0, 0.5, .10), labels = function(x) paste0(round(as.numeric(x*100)), "%"))

    推荐文章