title: R - Aggregate Percentage for Stacked Bar Charts using ggplot2
body: <p>I have some data that looks like the below. I'm aiming to generate stacked bar charts for them, but I need the values to be shown as percentages. I've managed to get as far as getting the data melted to the right shape and drawing the stacked bars, but the values go far beyond 100% (in my actual dataset, some values add up to 8000+). What is the correct way to setup ggplot2 so that I can create stacked bar charts in percentages?</p>
  
  <pre><code>#Raw Data
  x   A    B    C
  1   5   10   14
  1   4    4   14
  2   5   10   14
  2   4    4   14
  3   5   10   14
  3   4    4   14
  
  #Aggregate
  data &lt; read.table(...); 
  data &lt;- aggregate(. ~ x, data, sum) #&lt;---- Sum to Average? 
  x   A    B    C
  1   9   14   28
  2   9   14   28
  3   9   14   28
  
  #Melt Data
  data &lt;- melt(data,"x")
    x variable value
  1 1        A     9
  2 2        A     9
  3 3        A     9
  4 1        B    14
  5 2        B    14
  6 3        B    14
  7 1        C    28
  8 2        C    28
  9 3        C    28
  
  #Plot stack bar chart counts
  ggplot(data, aes(x=1, y=value, fill=variable)) + geom_bar(stat="identity") + facet_grid(.~x)
  </code></pre>
  
  <p>I'm hoping to get something like this before the melt so that I can melt it and plot that as a stacked bar chart, but I'm not sure how to approach this. </p>
  
  <pre><code>#Ideal Data Format - After Aggregate, Before Melt
  x     A       B       C
  1   17.64   27.45   54.90
  2   17.64   27.45   54.90
  3   17.64   27.45   54.90
  </code></pre>
  
  <hr>
  
  <p>Q: What is the correct way to create a stacked bar chart with percentages, using ggplot2? </p>
  
  <hr>