I'm trying to take event data (A, B, C, and D - below) which occur over 4 locations (1, 2, 3, 4 - below). I want to plot them as a stacked bar that is filled in to show the contribution of each event (A,B,C,D) to that location AND I want to show the integer values of those contributions. I would like to see not only the individual values (which below sort of does) but I'd also like to see the total contribution - which I can't figure out how to do.
So there are two problems: 1: Printing not only the individual values of a stacked bar but also (or even, separately / only) print the total value at the top. 2: The text labels get printed at a y offset of their value, so they overwrite each other and don't line up within the bar. I'd prefer them someplace expected inside a sub-bar such as the middle or top.
a <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,1,1,1,2)
b <- c('A','B','C','D','A','A','B','C','B','B','C','C','C','D','D','A','A','B','C','D')
df <- data.frame(a, b)
I want to create a summary of this - so here's table()
table(df$a, df$b)
A B C D
1 2 2 2 1
2 2 1 1 1
3 0 2 2 0
4 1 0 1 2
Now back to a data.frame for plotting with ggplot:
df2 <- data.frame(table(df$a, df$b))
Then plot it:
library(ggplot2)
ggplot(df2, aes(x=Var1, y=Freq, fill=Var2, label=Freq)) +
geom_bar(stat="identity") +
geom_text(stat="identity")
I would really appreciate help. Do I not need to wrangle my data frame through a table to summarize it and then back into a data frame? Can I get at the total height of the bar and print that label?
I feel like if I weren't using fill, I could get at the ..count.. value but stat="bin", but since I've gone to stat="identity" I can't seem to get at that summary value.
Thanks!