问题描述
我有一个图,需要从百分比数字中删除小数.数据已经四舍五入(两位小数),例如:
I have a plot where I need to delete the decimals from the percentage numbers. The data is already rounded (two decimals), an example of the data:
> head(df$X)
[1] 0.05 0.28 0.08 0.19 0.33
然后我用scale_x_continuous()
将其转换为百分比格式,但是正如您在下图中看到的那样,它们仍然有一个小数,我需要那些没有小数的数字.
And then I plot it, using scale_x_continuous()
to transform it to percentage format, but as you can see in the image below they still have one decimal and I need those numbers without decimals.
这是图形的代码:
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)
我找到的最接近的答案是这一个,但是我不知道不需要粘贴%"符号,因为我已经有了它.
The closest answer I have found is this one, but I don't need to paste the "%" symbol as I already have it.
推荐答案
scales::percent
具有您可以修改的accuracy
参数.这应该给您您想要的东西:
scales::percent
has an accuracy
argument that you can modify. This should give you what you want:
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))
这篇关于删除百分比轴中的小数-R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!