如何去掉这个 bar_plot 中的百分比?

How do I get rid of the percentages in this bar_plot?

我想在 ggplot2 中有一个闪避条形图,显示一个人是否有储蓄,所有这些都由另一个变量区域显示。由于缺少数据,我想将它们作为另一个类别包含在图中,这样每个区域都会有三个条:一个表示有,一个表示没有,一个表示缺少数据。现在我制作了一个图来准确显示我想要的内容,我在互联网上获取了一些代码,因为我真的无法正确地做到这一点。 唯一的问题是现在它在条形图的顶部显示百分比,这是我不想要的功能。 我该如何摆脱它?或者我怎样才能至少四舍五入?enter image description here


ggplot(adatok, aes(x=as.factor(region), fill=as.factor(savings)))+
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
  geom_text(aes(label=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

我将使用 mtcars(及其 cylgear)来演示两个选项。

起点:

library(ggplot)
data("mtcars")
ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+
  geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
  geom_text(aes(label=scales::percent(..count../tapply(..count.., ..x.. ,sum)[..x..]),
    y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
            stat="count", position=position_dodge(0.9), vjust=-0.5)+
  scale_y_continuous(labels = scales::percent)

  1. 如果您不想要百分号,请删除 scales::percent(并查看 0.72727272727272727 之类的数字)或 round(100*., 1) 它:

    ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+
      geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
      geom_text(aes(label=round(100*(..count../tapply(..count.., ..x.. ,sum)[..x..]), 1),
        y=..count../tapply(..count.., ..x.. ,sum)[..x..]  ),
                stat="count", position=position_dodge(0.9), vjust=-0.5)+
      scale_y_continuous(labels = scales::percent)
    

  2. 如果您根本不需要数字 ,请完全删除 geom_text(.)

    ggplot(mtcars, aes(x=as.factor(cyl), fill=as.factor(gear)))+
      geom_bar(aes( y=..count../tapply(..count.., ..x.. ,sum)[..x..]), position="dodge" ) +
      scale_y_continuous(labels = scales::percent)