Adding count labels to bar plots

2,956 views
Skip to first unread message

Bob

unread,
Nov 13, 2011, 11:28:56 AM11/13/11
to ggplot2
Hi All,

I'm adding text labels to a bar plot. I have it working in one form,
but I can't figure out why it's so much trouble when all I need is
access to the built-in ..count.. variable. Please take a look at the
simplified example below and tell me what I'm missing.

Thanks,
Bob

# Some simulated data:

Region <- factor( c("A","A","B","B","B") )
USfacts <- data.frame(Region)
USfacts

# Getting counts fro a bar chart manually:
myCounts <- as.data.frame( table(USfacts$Region ) )
myCounts

# Renaming
names(myCounts) <- c("Region", "Freq")
myCounts

# "Height" will be the height of my bar plot label
# so it should be slightly lower than the height
# of the bar itself.
myCounts$Height <- myCounts$Freq - 0.5
myCounts

# This plot does what I want, but it seems like
# it took a crazy amount of work to get here:

ggplot(myCounts, aes(Region, Freq)) +
geom_bar(fill = "white", color="black") +
geom_text(aes(Region, Height, label = Freq) )

# Here are the empty bars, ready to fill in labels.
# I don't need to specify ..count.., but I do so
# just to convince myself that it exists.

p <- ggplot(USfacts, aes(Region, ..count..)) +
geom_bar(fill = "white", color="black")
p

# Finally, I want to add the labels using ..count..
# that I thought was generated by the "bin" stat.
# ..count.. existed at one time, but now it's gone
# and bin does not seem to be regenerating it.

p + geom_text(stat="bin", y = ..count..-0.5, label=..count..)

# I'm missing a fundamental concept. What??

Winston Chang

unread,
Nov 13, 2011, 1:35:27 PM11/13/11
to Bob, ggplot2
Hi Bob - 

Here's what I'd suggest: You can remove one step, by doing the positional subtraction within ggplot. You can also use ddply to get the counts, instead of as.data.frame(table(...)). Personally, I find ddply easier to understand.

# Some simulated data:
Region <- factor( c("A","A","B","B","B") )
USfacts <- data.frame(Region)
USfacts

# Getting counts with ddply
myCounts <- ddply(USfacts, .(Region), summarise, Freq=length(Region))
myCounts

# Do the y subtraction within ggplot
ggplot(myCounts, aes(x=Region, y=Freq)) +

 geom_bar(fill = "white", color="black") +
 geom_text(aes(y=Freq-.5, label = Freq) )



-Winston



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Bob

unread,
Nov 13, 2011, 2:36:21 PM11/13/11
to ggplot2
That's a definite improvement. Any idea of why ..count.. seems
available for part of that plot but not the other? Thanks, Bob

Winston Chang

unread,
Nov 13, 2011, 2:47:02 PM11/13/11
to Bob, ggplot2
You need to put the mappings within aes() -- otherwise you're setting the properties instead of mapping variables to them. This code is even simpler than what I suggested before!

ggplot(USfacts, aes(Region, ..count..)) +
 geom_bar(fill = "white", color="black") +
 geom_text(stat="bin", aes(y = ..count..-0.5, label=..count..))

-Winston

Bob Muenchen

unread,
Nov 17, 2011, 6:11:32 PM11/17/11
to ggplot2
AAAAHHHHHH! How many times can I make that same basic error! Thanks
Winston.
Reply all
Reply to author
Forward
0 new messages