title: Error bars for barplot only in one direction, but for the last one in the opposite direction
body: <p>This very useful <a href="https://stackoverflow.com/questions/27585776/error-bars-for-barplot-only-in-one-direction">post</a> shows how to display error bars in one direction only with <code>ggplot</code>. To do this, the <code>geom_errorbar</code> function is modified as follows (as proposed by <a href="https://stackoverflow.com/users/4119353/sean-hughes">Sean Hughes</a>):</p>
  <pre><code>geom_uperrorbar &lt;- function(mapping = NULL, data = NULL,
     stat = &quot;identity&quot;, position = &quot;identity&quot;,
     ...,
     na.rm = FALSE,
     show.legend = NA,
     inherit.aes = TRUE) {
     layer(
        data = data,
        mapping = mapping,
        stat = stat,
        geom = GeomUperrorbar,
        position = position,
        show.legend = show.legend,
        inherit.aes = inherit.aes,
        params = list(
           na.rm = na.rm,
           ...
        )
     )
  }
  
  GeomUperrorbar &lt;- ggproto(&quot;GeomUperrorbar&quot;, Geom,
     default_aes = aes(colour = &quot;black&quot;, size = 0.5, linetype = 1, width = 0.5,
        alpha = NA),
  
     draw_key = draw_key_path,
  required_aes = c(&quot;x&quot;, &quot;y&quot;, &quot;ymax&quot;),
  
     setup_data = function(data, params) {
        data$width &lt;- data$width %||%
           params$width %||% (resolution(data$x, FALSE) * 0.9)
  
        transform(data,
           xmin = x - width / 2, xmax = x + width / 2, width = NULL
        )
     },draw_panel = function(data, panel_scales, coord, width = NULL) {
        GeomPath$draw_panel(data.frame(
           x = as.vector(rbind(data$xmin, data$xmax, NA, data$x,   data$x)),
           y = as.vector(rbind(data$ymax, data$ymax, NA, data$ymax, data$y)),
           colour = rep(data$colour, each = 5),
           alpha = rep(data$alpha, each = 5),
           size = rep(data$size, each = 5),
           linetype = rep(data$linetype, each = 5),
           group = rep(1:(nrow(data)), each = 5),
           stringsAsFactors = FALSE,
           row.names = 1:(nrow(data) * 5)
        ), panel_scales, coord)
     }
  )
  
  
  &quot;%||%&quot; &lt;- function(a, b) {
     if (!is.null(a)) a else b
  } 
  </code></pre>
  <p>But now I have the case that the last bar is negative. Thus, the error bar at the last bar must head in the other direction. Does anyone have an idea how this could work?</p>
  <p>Here is a reproducible example:</p>
  <pre><code>df &lt;- data.frame(trt = factor(c(1, 1, 2, 2)), resp = c(1, 5, 3, -2),
                   group = factor(c(1, 2, 1, 2)), se = c(0.1, 0.3, 0.3, 0.2))
  df2 &lt;- df[c(1,3), ]
  
  limits &lt;- aes(ymax = resp + se, ymin = resp - se)
  dodge &lt;- position_dodge(width = 0.9)
  
  p &lt;- ggplot(df, aes(fill = group, y = resp, x = trt))
  p + geom_bar(position = dodge, stat = &quot;identity&quot;) +
    geom_errorbar(limits, position = dodge, width = 0.25)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/ofBTc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ofBTc.png" alt="enter image description here" /></a></p>