Bob
unread,Nov 13, 2011, 11:28:56 AM11/13/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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??