title: Showing percentage in bars in ggplot
body: <p>I have a dataset with binary variables like the one below.</p>
  
  <pre><code>M4 = matrix(sample(1:2,20*5, replace=TRUE),20,5)
  M4 &lt;- as.data.frame(M4)
  M4$id &lt;- 1:20
  </code></pre>
  
  <p>I have produced a stacked bar plot using the code below</p>
  
  <pre><code>library(reshape)
  library(ggplot2)
  library(scales)
  M5 &lt;- melt(M4, id="id")
  M5$value &lt;- as.factor(M5$value)
  ggplot(M5, aes(x = variable)) + geom_bar(aes(fill = value), position = 'fill') +
    scale_y_continuous(labels = percent_format())
  </code></pre>
  
  <p>Now I want the percentage for each field in each bar to be displayed in the graph, so that each bar reach 100%. I have tried <a href="https://stackoverflow.com/questions/22231124/how-to-draw-stacked-bars-in-ggplot2-that-show-percentages-based-on-group">1</a>, <a href="https://stackoverflow.com/questions/20011266/label-selected-percentage-values-inside-stacked-bar-plot-ggplot2">2</a>, <a href="https://stackoverflow.com/questions/12386005/r-stacked-percentage-bar-plot-with-percentage-of-binary-factor-and-labels-with">3</a> and several similar questions, but I can't find any example that fits my situation. How can I manage this task? </p>