如何使用 ggplot 以 2 位数输入条形图百分比 (%)?

How to put in bar plot percent (%) with 2 digits with ggplot?

我在 R 方面的技能不多,我有两个问题。 有人可以帮我解决这个问题吗?

a) 我想在此图形中使用两位数的百分比数字,例如 (120,07%)。我一直在尝试使用“dplyr”,但我做不到。

b ) 那么,我可以用图形中的百分比更改“y 轴”吗?

谢谢!

library(ggplot2)
ggplot(place, aes(x = Place, y = Dif)) +
  geom_bar(aes(fill = Dif < 0), colour="black", stat = "identity") +
  scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("gray", "red")) +
  geom_text(aes(label=Dif), position=position_dodge(width=0.9), vjust=-0.25)
structure(list(Place = c("Supermarket", "Markets", 
"House", "Consumption", "Hipermarkets", "Comerce", 
"Outdoors sale"), Dif = c(-20.1884514229122, -50.0150282227513, 
5.34342366569214, -2.47231788994851, 25.7466309314144, 120.078289871755, 
24.0501027574048)), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"))

你可以这样做:

library(ggplot)

ggplot(df, aes(Place, Dif/100, fill = Dif > 0)) + 
  geom_col() +
  geom_text(aes(y = Dif/100 + 0.1 * sign(Dif/100),
                label = scales::percent(Dif/100))) +
  geom_hline(yintercept = 0) +
  scale_fill_manual(values = c("red", "forestgreen"), guide = guide_none()) +
  scale_y_continuous(labels = scales::percent, name = "Diff") +
  theme_bw()