Center labels stacked bar (counts) ggplot2
There's some extraneous ddply
action going on with this one as I knew the solution I wanted but was having trouble keeping the frequencies aligned with the positions, but I think the algorithm for finding the group midpoints is worth posting:
group_midpoints = function(g) {
cumsums = c(0, cumsum(g$Freq))
diffs = diff(cumsums)
pos = head(cumsums, -1) + (0.5 * diffs)
return(data.frame(cyl=g$cyl, pos=pos, Freq=g$Freq))
}
dat3 = ddply(dat, .(gear), group_midpoints)
ggplot(dat3, aes(x = gear, fill = cyl)) +
geom_bar(aes(weight=Freq), position="stack") +
geom_text(position = "identity", aes(x = gear, y = pos, ymax = 15, label = cyl), size=4)