Using ..count..

1,193 views
Skip to first unread message

Ravina, Mark

unread,
May 2, 2014, 9:25:34 AM5/2/14
to ggp...@googlegroups.com
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






Thomas

unread,
May 2, 2014, 11:14:33 AM5/2/14
to ggp...@googlegroups.com
This is from stackoverflow http://stackoverflow.com/questions/2551921/show-frequencies-along-with-barplot-in-ggplot2


With some minor modifications...

stuff<-data.frame(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))
)

p <- ggplot(stuff, aes(factor(people), fill=food))
p + geom_bar() + 
stat_bin(aes(label=paste("n = ",..count..)), vjust=1, geom="text")

Ravina, Mark

unread,
May 7, 2014, 8:53:36 AM5/7/14
to Thomas, ggp...@googlegroups.com
Thanks, Thomas . . . This works perfectly. 

 

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages