Pet*_*etr 1 r ggplot2 geom-text
我需要固定的小数位数(在这种情况下为两个),但我无法使其工作,我知道使用round和accuracy功能,但它似乎对我不起作用
代码:
library(ggplot2)
ggplot(mtcars, aes(factor(cyl))) +
geom_bar(color = "steelblue", fill = "#00AFBB", na.rm = T) +
scale_fill_discrete(drop=FALSE) +
scale_x_discrete(drop=FALSE) +
geom_text(aes(label=scales::percent(round(..count../sum(..count..),4))),
stat='count',vjust = -0.5, size = 4)
Run Code Online (Sandbox Code Playgroud)
这是内置于scales::percent. 有一个accuracy参数被描述为“要四舍五入的数字”。
ggplot(mtcars, aes(factor(cyl))) +
geom_bar(color = "steelblue", fill = "#00AFBB", na.rm = T) +
scale_fill_discrete(drop=FALSE) +
scale_x_discrete(drop=FALSE) +
geom_text(aes(label=scales::percent(..count../sum(..count..), accuracy = 0.01)),
stat='count',vjust = -0.5, size = 4)
Run Code Online (Sandbox Code Playgroud)