title: R: Adding number of observation on the barplot with geom_text
body: <p>I know that this question has been asked very often, but I tried all the method I found and none of them seems to work..</p>
  
  <p>This is my current data.</p>
  
  <pre><code>df &lt;- data.frame(ID = c(1,2,3,4), Type = c("A","B","A","B"), Score1 = c(10,20,30,40), Score2 = c(20,40,60,80))
  ID   Type    Score1       Score2
  1       A        10           20
  2       B        20           40
  3       A        30           60
  4       B        40           80
  </code></pre>
  
  <p>and now I want to make a graph that looks like this
  Edit: I placed the wrong graph > It should look like this</p>
  
  <p><a href="https://i.stack.imgur.com/fsvlq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fsvlq.jpg" alt="enter image description here"></a></p>
  
  <p>I am able to achieve the bar graph using the <code>reshape</code> and <code>ggplot</code></p>
  
  <pre><code>rawscore &lt;- df[, c("Type","Score1", "Score2")]
  rawscore &lt;- melt(rawscore, id = c("Type"))
  ggplot(rawscore, aes(type, value, fill=variable))+
  geom_bar(stat="summary", fun.y="mean", position="dodge")
  </code></pre>
  
  <p>However, I struggles to add the number of observation on the graph
  I know that I should use  <code>geom_text</code> to put the label on the graph, so I tried creating the new vector from this <a href="https://stackoverflow.com/questions/15720545/use-stat-summary-to-annotate-plot-with-number-of-observations">post</a></p>
  
  <pre><code>nlabels &lt;- table(Type)
  </code></pre>
  
  <p>but I got an error saying</p>
  
  <pre><code>Error: Aesthetics must be either length 1 or the same as the data
  </code></pre>
  
  <p>Any suggestions?</p>