为什么 stat_summary 不添加条形图上的标签?

Why would stat_summary not add up the labels on my barplot?

我有一个汇总数据集,如下所示:

place<-c('PHF','Mobile clinic','pharmacy','PHF','pharmacy','PHF','normal shop','pharmacy')
District<-c('District1','District1','District1','District2','District2','District3','District3','District3')
cat<-c('public','public','private','public','private','public','private','private')
Freq<-c(7,2,5,4,7,5,1,8)
Q14_HH<-data.frame(place,District,cat,Freq)

我创建了一个看起来不错的条形图:

plot<- ggplot(data = Q14_HH, aes(x=place,y=Freq,fill=cat)) +
  geom_bar(stat='identity') +
  labs(title="Where do you get your medicines from normally? (human)", 
       subtitle='Precentage of households, n=30',x="", y="Percentage",fill='Outlet type') +
  theme(axis.text.x = element_text(angle = 90, hjust=1,vjust=0.5))

现在我想把频率的总和放在每个条的顶部,即对于每个位置变量:

plot+ stat_summary(geom='text',aes(label = Freq,group=place),fun=sum)

但由于某种原因,它不会计算总和。我收到一条警告消息: 删除了包含缺失值的 2 行 (geom_text)

有人可以帮助我了解这里发生的事情吗?

在计算 sum 时,您必须使用 label=..y..label=after_stat(y) 将计算出的 y 值映射到 label aes 上:

library(ggplot2)

plot <- ggplot(data = Q14_HH, aes(x = place, y = Freq, fill = cat)) +
  geom_bar(stat = "identity") +
  labs(
    title = "Where do you get your medicines from normally? (human)",
    subtitle = "Precentage of households, n=30", x = "", y = "Percentage", fill = "Outlet type"
  ) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

plot + stat_summary(geom = "text", aes(label = ..y.., group = place), fun = sum)