2

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()

enter image description here

regards, Lin

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 1
    Check out `geom_bar` from the `ggplot2` package. `geom_text` and `hist` are not mean to be used together. – Mike H. Aug 04 '16 at 23:25
  • @MikeyMike, thanks, wondering if any solution if I use `hist`? – Lin Ma Aug 04 '16 at 23:36
  • @MikeyMike, updated my code example, it seems `geom_bar ` does not work good for my example. Vote up and your advice is appreciated. – Lin Ma Aug 04 '16 at 23:48
  • 4
    Possible duplicate (if you want a histogram): http://stackoverflow.com/q/27593888/903061 – Gregor Thomas Aug 05 '16 at 00:13
  • 2
    Possible dupe for the bar plot: http://stackoverflow.com/q/29869862/903061 – Gregor Thomas Aug 05 '16 at 00:14
  • 1
    I think @Gregor's first comment is what you wanted. – shayaa Aug 05 '16 at 02:37
  • @Gregor, thanks and vote up. I read the first post, and confused by what means `aes(y = (..count..)/sum(..count..)`, especially what means `..count..`? Tried to find help in R but failed. If you could comment, it will be great. – Lin Ma Aug 05 '16 at 04:46
  • @shayaa, vote up for the reply. I read the first sample, and confused by what means `aes(y = (..count..)/sum(..count..)`, especially what means `..count..`? Tried to find help in R (by `? ..count..`) but failed. If you could comment, it will be great. – Lin Ma Aug 05 '16 at 04:47
  • @Gregor, for the 2nd example, I tried and it works, but confused by statement `aes(x= test2, group=test1))`, in R help, I do not find for `aes`, there is a parameter called `group`, could you elaborate what `group` means? Thanks. – Lin Ma Aug 05 '16 at 04:48

1 Answers1

7

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)

enter image description here

To your question about ..count.. see: Special variables in ggplot (..count.., ..density.., etc.).

Community
  • 1
  • 1
Mike H.
  • 13,960
  • 2
  • 29
  • 39