title: Centre error bars on geom_bar and centre bars on label axis
body: <p>I have a different number of &quot;fill&quot; options in my dataset and error bars for only one of these options. I've come up with 2 variations and either the bars are not centered over the x-axis label or the error bar is not displayed over the correct bar. Is there any way to create a bar plot where each bar is the same width, centered on the x-axis label and the error bar associated with the type is centered over the bar?</p>
  <p>Ideally there is also a small space between the bars like in fig_2 example below.</p>
  <p>Some sample data:</p>
  <pre><code>sample.data &lt;- data.frame(Type = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot;, &quot;b&quot;),
                     Value = c(63, 123, 73, 79, 94),
                     Name = c(&quot;Item A&quot;,&quot;Item A&quot;,&quot;Item A&quot;, &quot;Item B&quot;, &quot;Item B&quot;),
                     SD = c(3, NA, NA, 5, NA)) 
  </code></pre>
  <p>A plot where the error bars are centered above the correct bar and all bars are the same width, but the bars are not centered over the X axis label.</p>
  <pre><code>
  fig_1 &lt;- sample.data %&gt;% 
    ggplot(aes(y = Value, x = Name, fill = Type)) +
    geom_bar(stat=&quot;identity&quot;,position=position_dodge(preserve=&quot;single&quot;),
             width = 0.8) +
    geom_errorbar(stat=&quot;Identity&quot;,
                  position=position_dodge(0.8, preserve = &quot;single&quot;),
                  aes(ymin=Value-SD, ymax=Value+SD), width=.2)
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Kqmu9.png" rel="nofollow noreferrer">fig_1</a></p>
  <p>A plot where the bars are centered over the axis label and are all the same width, but error bars are not centered over the correct bar.</p>
  <pre><code>fig_2 &lt;- sample.data %&gt;% 
    ggplot(aes(y = Value, x = Name, fill = Type)) +
    geom_bar(stat=&quot;identity&quot;,position=position_dodge2(preserve=&quot;single&quot;),
             width = 0.8) +
    geom_errorbar(stat=&quot;Identity&quot;,
                  position=position_dodge2(0.8, preserve = &quot;single&quot;),
                  aes(ymin=Value-SD, ymax=Value+SD), width=.2)
  
  fig_2
  
  </code></pre>
  <p><a href="https://i.stack.imgur.com/AZkg3.png" rel="nofollow noreferrer">fig_2</a></p>
  <p>Edit:</p>
  <p>This is what I want my figure to look like:
  <a href="https://i.stack.imgur.com/1k5pP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1k5pP.png" alt="This is what I want my figure to look like" /></a></p>
  <p>Edit 2:</p>
  <p>Randomized data similar to my own:</p>
  <pre><code>sample.data.2 &lt;- data.frame('Fruit' = c('Apple', 'Banana', 'Mango', 'Orange', 'Pear', 'Banana', 'Apple', 'Banana', 'Pear', 'Banana', 'Apple', 'Banana', 'Mango', 'Orange', 'Pear'),
  'Source' =  c('A', 'A', 'A', 'A', 'A', 'B', 'C', 'C', 'C', 'D', 'E', 'E', 'E', 'E', 'E'),
  'SD' =  c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.08, 0.09, 0.07, 0.02, 0.09 ),
  'Cost' =  c(12.05, 10.5, 10.46, 9.14, 11.34, 10, 11.43, 12.86, 12.69, 9.42, 9.74, 10.01, 11.98, 9.31, 12.2))
  </code></pre>
  <p>Edit 3:</p>
  <p>dput of sample.data</p>
  <pre><code>structure(list(Type = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, 
  &quot;a&quot;, &quot;c&quot;), Value = c(63, 123, 73, 79, 94, 84, 48, 68, 75, 68), 
      Name = c(&quot;Item A&quot;, &quot;Item A&quot;, &quot;Item A&quot;, &quot;Item B&quot;, &quot;Item B&quot;, 
      &quot;Item D&quot;, &quot;Item D&quot;, &quot;Item D&quot;, &quot;Item C&quot;, &quot;Item C&quot;), SD = c(3, 
      NA, NA, 5, NA, 7, NA, NA, 4, NA)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -10L))
  </code></pre>
  <p>dput of sample.data.2</p>
  <pre><code>structure(list(Fruit = c(&quot;Apple&quot;, &quot;Banana&quot;, &quot;Mango&quot;, &quot;Orange&quot;, 
      &quot;Pear&quot;, &quot;Banana&quot;, &quot;Apple&quot;, &quot;Banana&quot;, &quot;Pear&quot;, &quot;Banana&quot;, &quot;Apple&quot;, 
      &quot;Banana&quot;, &quot;Mango&quot;, &quot;Orange&quot;, &quot;Pear&quot;), Source = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, 
      &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;, &quot;E&quot;), 
          SD = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.08, 0.09, 
          0.07, 0.02, 0.09), Cost = c(12.05, 10.5, 10.46, 9.14, 11.34, 
          10, 11.43, 12.86, 12.69, 9.42, 9.74, 10.01, 11.98, 9.31, 
          12.2)), class = &quot;data.frame&quot;, row.names = c(NA, -15L))
  </code></pre>