Wondering if geom_text
works for hist
? Tried following code and it seems no effect. I just want to show label (the number of elements belonging to a specific histogram bucket), when plotting each bar for each histogram bucket. Any solutions are appreciated. Thanks.
p <- hist(df$foo,
main="title",xlab="foo")
p + geom_text()
Edit 1, tried geom_bar
, here is my code and it seems not working well, since I expect a number labelled on each bar. In the diagram, it only shows 2.5, 5, 7.5 and 10, I expect to show 1, 2, 3, ..., 9, 10 for each bar.
g <- ggplot(df, aes(df$foo))
g + geom_bar()
regards,
Lin
Since no one has answered this I'll give it a try:
First, a few tips:
- posting your data will make it more likely for people to answer (so we don't have to replicate it)
- Doing some research on your own goes a long way. For example, you mention that you want a label for each bar - fine there are several ways to do it. A quick google search digs up this: Customize axis labels
Now to actually answer your question:
set.seed(1)
#Make sample data since none is provided
df <- data.frame(foo=sample(1:10,200,replace=T))
#This is what you want - use as.factor(foo) - this gives you the breaks at every bar.
g <- ggplot(df, aes(as.factor(foo)))
#Actually making the barplot and adding labels to it
g + geom_bar() +stat_count(aes(y=..count..,label=..count..),geom="text",vjust=-1)
To your question about ..count..
see: Special variables in ggplot (..count.., ..density.., etc.).