我使用这段代码制作了以下堆叠条形图:
library(ggplot2)
library(dplyr)
Year <- c(rep(c("2006", "2007", "2008", "2009"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- data.frame(Year, Category, Frequency)
Data2 <-Data%>%
group_by(Year, Category)%>%
summarise(Sum_grp = sum(Frequency))
Data3 <-transform(Data2, Pos = ave(Frequency, Year, FUN = cumsum) - Frequency / 2)
ggplot(Data3, aes(Year, Frequency, group=Category,fill = Category))+
geom_bar(stat="identity")+
geom_text(aes(label = Frequency,y=Pos), size = 3)
现在,我想在每个柱形图的顶部添加每组的总和,但我不知道该怎么做。
有人能帮帮我吗?
非常感谢!!!!