删除小数y轴ggplot2

KGB*_*B91 3 r ggplot2

我有这样的数据,df_Filtered:

Product Relative_Value
Car     0.12651458
Plane   0.08888552
Tank    0.03546231
Bike    0.06711630
Train   0.06382191
Run Code Online (Sandbox Code Playgroud)

我想在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())
Run Code Online (Sandbox Code Playgroud)

如何摆脱图表中y轴的小数?所以它说20 %而不是20.0 %

www*_*www 12

使用percent_formatscales包中设置accuracy为2.

library(ggplot2)
library(scales)

ggplot(df_Filtered, aes(x = Product, y = Relative_Value, fill = Product)) +
  scale_y_continuous(labels = percent_format(accuracy = 2)) +
  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()) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

数据

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)
Run Code Online (Sandbox Code Playgroud)

  • 刚刚检查这个!:)有趣的是`percent_format(1)`在y轴上显示5%而不是4%. (3认同)