gpt4 book ai didi

删除小数点y轴ggplot2

转载 作者:行者123 更新时间:2023-12-04 10:11:34 25 4
gpt4 key购买 nike

我有类似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 %吗?

最佳答案

使用percent_format包中的scalesaccuracy设置为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)

关于删除小数点y轴ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com