this question has answer here:
i used code make following stacked bar chart:
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)
now, add sum of each group on top each bar not have idea how.
could me please?
thanks lot!!!!!
if wanted avoid making 3rd summary dataset, use stat_summary
.
ggplot(data3, aes(year, frequency, group = category, fill = category))+ geom_bar(stat="identity")+ geom_text(aes(label = frequency,y=pos), size = 3) + stat_summary(fun.y = sum, aes(label = ..y.., group = year), geom = "text")
use vjust
move labels more if needed. found vjust = -.2
seemed pretty good.
Comments
Post a Comment