title: Position problem with geom_bar when using both width and dodge
body: <p>I have the following data frame</p>
  
  <pre><code>group1 = c('a', 'b')
  group2 = c('1', '1', '2', '2')
  mean = 1:4
  sd = c(0.2, 0.3, 0.5, 0.8)
  df = data.frame(group1, group2, mean, sd)
  </code></pre>
  
  <p>I want to plot the sd on the graph, either with <code>geom_errorbar()</code>. This works perfectly:</p>
  
  <pre><code>ggplot(data = df, aes(x=group1, y = mean))+
    geom_col(position = 'dodge') + 
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd),
                  position = 'dodge')
  </code></pre>
  
  <p>As I wanted to reduce the width of the error bars I run:</p>
  
  <pre><code>ggplot(data = df, aes(x=group1, y = mean))+
    geom_col(position = 'dodge') + 
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2,
                  position = 'dodge')
  </code></pre>
  
  <p>So far so good. But then I wanted to fill by group2.</p>
  
  <pre><code>ggplot(data = df, aes(x=group1, y = mean, fill = group2))+
    geom_col(position = 'dodge') + 
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2,
                  position = 'dodge')
  </code></pre>
  
  <p>The problem is that the error bars are no longer in the middle of bars. I have no idea why. I looked at the documentation but did not find anything on this issue. I looked at this question     <a href="https://stackoverflow.com/questions/31092988/force-error-bars-to-be-in-the-middle-of-bar">Force error bars to be in the middle of bar</a>
  and this one     <a href="https://stackoverflow.com/questions/47432681/aesthetics-issue-using-position-dodge-in-ggplots-with-geom-errorbar">Aesthetics issue using position dodge in ggplots with geom_errorbar</a>   but no one explained why this occurs. One proposed solution is to add     position_dodge(0.9). </p>
  
  <pre><code>ggplot(data = df, aes(x=group1, y = mean, fill = group2))+
    geom_col(position = 'dodge') + 
    geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.2,
                  position = position_dodge(0.9))
  </code></pre>
  
  <p>It worked but I have no idea why and what it did. Can someone explain what is going on? Why can't I just add width = 0.2 to reduce the width of the error bar? And what does position_dodge(0.9) do? Why do I need it? Why the problem occurs only when I added fill = group2? </p>