title: How can I create a (100%) stacked histogram in R?
body: <h3>My dataset:</h3>
  
  <p>I have data in the following format (here, imported from a CSV file). You can find an example dataset as CSV <a href="http://cl.ly/3F1u0W2b3y081v172A2O" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>PAIR   PREFERENCE
  1      5
  1      3
  1      2
  2      4
  2      1
  2      3
  </code></pre>
  
  <p>… and so on. In total, there are 19 pairs, and the <code>PREFERENCE</code> ranges from <code>1</code> to <code>5</code>, as discrete values.</p>
  
  <hr>
  
  <h3>What I'm trying to achieve:</h3>
  
  <p>What I need is a stacked histogram, e.g. a 100% high column, for each pair, indicating the distribution of the <code>PREFERENCE</code> values.</p>
  
  <p>Something similar to the "100% stacked columns" in Excel, or (although not quite the same, a so-called "mosaic plot"):</p>
  
  <p><img src="https://i.stack.imgur.com/RrA9Q.jpg" width="200"/></p>
  
  <hr>
  
  <h3>What I tried:</h3>
  
  <p>I figured it'd be easiest using <code>ggplot2</code>, but I don't even know where to start. I know I can create a simple bar chart with something like:</p>
  
  <pre><code>ggplot(d, aes(x=factor(PAIR), y=factor(PREFERENCE))) + geom_bar(position="fill")
  </code></pre>
  
  <p>… that however doesn't get me very far. So I tried this, and it gets me somewhat closer to what I'm trying to achieve, but it still uses the count of <code>PREFERENCE</code>, I suppose? Note the <code>ylab</code> being "count" here, and the values ranging to 19.</p>
  
  <pre><code>qplot(factor(PAIR), data=d, geom="bar", fill=factor(PREFERENCE_FIXED))
  </code></pre>
  
  <p>Results in:</p>
  
  <p><img src="https://i.stack.imgur.com/SyKxv.png" alt="enter image description here"></p>
  
  <ul>
  <li>So, what do I have to do to get the stacked bars to represent a histogram? </li>
  <li>Or do they actually do this already? </li>
  <li>If so, what do I have to change to get the labels right (e.g. have percentages instead of the "count")?</li>
  </ul>
  
  <p><sub>By the way, this is not really related to <a href="https://stackoverflow.com/q/2405575/435093">this question</a>, and only marginally related to <a href="https://stackoverflow.com/questions/2241290/stacked-area-histogram-in-r">this</a> (i.e. probably same idea, but not continuous values, instead grouped into bars).</sub></p>