I’m trying access total observations for subgroups using ..count.., with no success
people <- c(rep("Klingon",9), rep("Vulcan",9),rep("Cardassian", 14))
food <-c(rep("Breakfast",3),rep("Lunch",4),rep("Dinner",6),rep("Breakfast",7),rep("Lunch",3),rep("Dinner",5),rep("Lunch",4))
stuff <- data.frame(cbind(people, food))
ggplot(stuff, aes(x=people, fill=food)) + geom_bar()
this produces a fine bar chart, but what if I want to label the bar with the counts?
ggplot is calculating the counts, but how do I access those summaries?
If I run:
ggplot(stuff, aes(x=people, y=..count.., fill=food)) + geom_bar() + geom_text(aes(y=..count..))
Error in do.call("layer", list(mapping = mapping, data = data, stat = stat, :
object '..count..' not found
Strangely, this code works, without labels
ggplot(stuff, aes(x=people, y=..count.., fill=food, label=..count..)) + geom_bar()
But the following generates an error
> ggplot(stuff, aes(x=people, y=..count.., fill=food, label=..count..)) + geom_bar() + geom_text(aes(y=..count..))
Error in eval(expr, envir, enclos) : object 'count' not found