title: Issue with ggplot2, geom_bar, and position="dodge": stacked has correct y values, dodged does not
body: <p>I'm having quite the time understanding <code>geom_bar()</code> and <code>position="dodge"</code>. I was trying to make some bar graphs illustrating two groups. Originally the data was from two separate data frames. Per <a href="https://stackoverflow.com/questions/8901330/multiple-histograms-with-ggplot2-position">this question</a>, I put my data in long format. My example:</p>
  
  <pre><code>test &lt;- data.frame(names=rep(c("A","B","C"), 5), values=1:15)
  test2 &lt;- data.frame(names=c("A","B","C"), values=5:7)
  
  df &lt;- data.frame(names=c(paste(test$names), paste(test2$names)), num=c(rep(1, 
  nrow(test)), rep(2, nrow(test2))), values=c(test$values, test2$values))
  </code></pre>
  
  <p>I use that example as it's similar to the spend vs. budget example. Spending has many rows per <code>names</code> factor level whereas the budget only has one (one budget amount per category).</p>
  
  <p>For a stacked bar plot, this works great:</p>
  
  <pre><code>ggplot(df, aes(x=factor(names), y=values, fill=factor(num))) +
  geom_bar(stat="identity")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/UYT9p.png" alt="stacked plot"></p>
  
  <p>In particular, note the y value maxes. They are the sums of the data from <code>test</code> with the values of <code>test2</code> shown on blue on top.</p>
  
  <p>Based on other questions I've read, I simply need to add <code>position="dodge"</code> to make it a side-by-side plot vs. a stacked one:</p>
  
  <pre><code>ggplot(df, aes(x=factor(names), y=values, fill=factor(num))) + 
  geom_bar(stat="identity", position="dodge")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/mo77U.png" alt="dodged"></p>
  
  <p>It looks great, but note the new max y values. It seems like it's just taking the max y value from each names factor level from <code>test</code> for the y value. It's no longer summing them.</p>
  
  <p>Per some other questions (like <a href="https://stackoverflow.com/questions/3773969/ggplot-position-dodge-question">this one</a> and <a href="https://stackoverflow.com/questions/11005782/ggplot2-geom-bar-position-dodge-does-not-dodge">this one</a>, I also tried adding the <code>group=</code> option without success (produces the same dodged plot as above):</p>
  
  <pre><code>ggplot(df, aes(x=factor(names), y=values, fill=factor(num), group=factor(num))) +
  geom_bar(stat="identity", position="dodge")
  </code></pre>
  
  <p>I don't understand why the stacked works great and the dodged doesn't just put them side by side instead of on top.</p>
  
  <hr>
  
  <p><strong>ETA:</strong> I found a <a href="https://groups.google.com/forum/?fromgroups#!topic/ggplot2/tdiL-Gq3ThQ" rel="noreferrer">recent question</a> about this on the ggplot google group with the suggestion to add <code>alpha=0.5</code> to see what's going on. It isn't that ggplot is taking the max value from each grouping; it's actually over-plotting bars on top of one another for each value.</p>
  
  <p>It seems that when using <code>position="dodge"</code>, ggplot expects only one y per x. I contacted Winston Chang, a ggplot developer about this to confirm as well as to inquire if this can be changed as I don't see an advantage. </p>
  
  <p>It seems that <code>stat="identity"</code> should tell ggplot to tally the <code>y=val</code> passed inside <code>aes()</code> instead of individual counts which happens without <code>stat="identity"</code> and when passing no y value.</p>
  
  <p>For now, the workaround seems to be (for the original df above) to aggregate so there's only one y per x:</p>
  
  <pre><code>df2 &lt;- aggregate(df$values, by=list(df$names, df$num), FUN=sum)
  p &lt;- ggplot(df2, aes(x=Group.1, y=x, fill=factor(Group.2)))
  p &lt;- p + geom_bar(stat="identity", position="dodge")
  p
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/0wz6m.png" alt="correct"></p>