I'm having a problem formatting ggplot2 to display a stacked bar plot with cumulative percent on the Y axis and counts within the bars. I can do one plot of each type (one with percent on the Y axis, one with counts in the bars) but not both. Here's what I have:
group <- c(1,1,1,2,2,2)
ind <- c(1,2,3,1,2,3)
count <- c(98,55,10,147,31,3)
df <- data.frame(group, ind, count)
library(ggplot2)
library(scales)
ggplot(df, aes(y=count, x=factor(group), fill=factor(ind), label=cfreq)) +
geom_bar(stat = "identity") + ylab("Percent Level 1 Classes") +
scale_fill_discrete(name="Level 1\nClasses") +
xlab("Level 2 Groups") +
geom_text(size = 3, position = position_stack(vjust = 0.5))
This produces the following plot with counts but no percent on Y axis:
The second version of the plot produces the percent on the Y axis but no counts in the bars:
ggplot(df, aes(y=count, x=factor(group), fill=factor(ind))) +
geom_bar(position = "fill", stat = "identity") +
ylab("Percent Level 1 Classes") +
scale_fill_discrete(name="Level 1\nClasses") +
xlab("Level 2 Groups")
But I can't get it to do both. Rather than waste space, I did try "label=cfreq" in the "aes" statement to no avail--seems to conflict with the "geom_text" option. Any help would be greatly appreciated.