title: R stacked percentage bar plot with percentage of binary factor and labels (with ggplot)
body: <p>I want to produce a graphic that looks something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/vGdPn.png" alt="enter image description here"></p>
  
  <p>My original data set looks something like this:</p>
  
  <pre><code>&gt; bb[sample(nrow(bb), 20), ]
        IMG QUANT FIX
  25663   1     1   0
  7936    2     2   0
  23586   3     2   0
  23017   2     2   1
  31363   1     3   1
  7886    2     2   0
  23819   3     3   1
  29838   2     2   1
  8169    2     3   1
  9870    2     3   0
  31440   2     1   0
  35564   3     1   0
  24066   1     2   0
  12020   3     2   0
  6742    3     2   0
  6189    2     3   0
  26692   2     3   0
  1387    3     2   0
  31839   2     3   1
  28637   3     2   0
  </code></pre>
  
  <p>So the idea is that the bars display where <code>FIX = 1</code> per factor <code>QUANT</code> and per
  factor <code>IMG</code>.</p>
  
  <p>I've aggregated my data set into percentages using <code>plyr</code></p>
  
  <pre><code>library(plyr)
  bb.perc &lt;- ddply(bb,.(QUANT,IMG),summarise,FIX.PROP = sum(FIX) / length(FIX))
  </code></pre>
  
  <p>It does <em>almost</em> the right thing:</p>
  
  <pre><code>  QUANT IMG   FIX.PROP
  1     1   1 0.52439024
  2     1   2 0.19085366
  3     1   3 0.13658537
  4     2   1 0.20414201
  5     2   2 0.53964497
  6     2   3 0.09585799
  7     3   1 0.29000000
  8     3   2 0.13000000
  9     3   3 0.40705882
  </code></pre>
  
  <p>But now if I make a graph, it doesn't account for the <code>FIX==0</code> cases, i.e. all bars have the same height, namely 100%, which isn't what I want. Note how the individual QUANT subframes don't add up to 100%:</p>
  
  <pre><code>&gt; sum(bb.perc[1:3,]$FIX.PROP)
  [1] 0.8518293
  &gt; sum(bb.perc[4:6,]$FIX.PROP)
  [1] 0.839645
  &gt; sum(bb.perc[7:9,]$FIX.PROP)
  [1] 0.8270588
  </code></pre>
  
  <p>The best I could do with R is to display counts:</p>
  
  <pre><code># Take only the positive samples
  bb.pos &lt;- bb[bb$FIX == 1,]
  # Plot the counts
  ggplot(bb,aes(factor(QUANT),fill=factor(IMG))) + geom_bar() +
    scale_y_continuous(labels=percent)
  </code></pre>
  
  <p>And results in:
  <img src="https://i.stack.imgur.com/z5pkz.png" alt="enter image description here">
  This is also not what I want:</p>
  
  <ul>
  <li>The percentage scale is way off. I need a way to pass the 100% point to the
  <code>percent</code> function, but I have no idea how.</li>
  <li>It lacks the labels.</li>
  </ul>
  
  <p>There are a great deal of <em>similar</em> questions on SO already, but I seem to lack
  the sufficient amount of intelligence (or understanding of R) to extrapolate
  from them to a solution to my particular problem.</p>
  
  <p>Thanks for any pointers!</p>
  
  <p>EDIT: Sven Hohenstein provided an answer already, but here's how I ended up doing it myself as well:</p>
  
  <pre><code>&gt; ggplot(bb.perc,aes(x=factor(QUANT),y=FIX.PROP,label=paste(round(FIX.PROP*100),
       "%"),fill=factor(IMG)))+ geom_bar(stat="identity") + geom_text(position="stack",
       aes(ymax=1),vjust=5) + scale_y_continuous(labels = percent)
  </code></pre>
  
  <p>Using the <code>bb.perc</code> that I defined further up using <code>plyr</code>. This one has the
  advantage that the percentages are computed locally per column, and not
  globally.</p>
  
  <p>Thanks everyone for the help. The following two questions and their respective
  answers helped me greatly in getting it right:</p>
  
  <p><a href="https://stackoverflow.com/questions/8243621/stacked-bar-graph-labels-with-ggplot2">Stacked Bar Graph Labels with ggplot2</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11653268/adding-labels-to-ggplot-bar-chart">Adding labels to ggplot bar chart</a></p>
  
  <p>What I did wrong initially, was pass the <code>position = "fill"</code> parameter to
  <code>geom_bar()</code>, which for some reason made all the bars have the same height!</p>