title: R - ggplot2 - Add arrow if geom_errorbar outside limits when x-axis is a factor variable
body: <p>I want to use geom_segment to replace error bars with arrows when the error exceeds a certain limit. I found a previous post that addresses this question: <a href="https://stackoverflow.com/questions/28415457/r-ggplot2-add-arrow-if-geom-errorbar-outside-limits">R - ggplot2 - Add arrow if geom_errorbar outside limits</a></p>
  
  <p>The code works well, except that my x-axis is a <strong>factor variable</strong> instead of a numeric variable. Using position_dodge within the geom_segment statement makes the arrows start in the correct location, but it doesn't change the terminal point (xend) and <strong>all arrows point towards one central point on the x-axis instead of going straight up from the origins.</strong> </p>
  
  <p>Instead of recoding the x-axis to be numeric (I will use this code to create many plots that have a range of x-axis values, with the last numeric value always ending in "+"), is there a way to correct this within geom_segment?</p>
  
  <p>Code used:</p>
  
  <pre><code>data$OR.95U_u = ifelse(data$OR.95U &gt; 10, 10 , NA)
  
  ggplot(data, aes(x = numAlleles, y = OR, fill = Outcome)) + 
    geom_bar(position = position_dodge(.5), stat = "identity", width = .4, color = "black") + geom_hline(yintercept = 1, linetype = "dashed", color = "black") + 
    ylim(0,10) + geom_errorbar(aes(ymin=OR.95L, ymax=OR.95U), width=.2,position=position_dodge(.5)) +
    theme(legend.key = element_blank(), text = element_text(size = 11.5), legend.title = element_blank()) + 
    labs(x = "Number of rare alleles") +
    scale_fill_manual(values=c("chocolate1","coral1", "red2", "darkred")) + 
    geom_segment(aes(x = numAlleles, xend = numAlleles, y = OR, yend = OR.95U_u), position = position_dodge(.5), arrow = arrow(length = unit(0.3, "cm")))
  </code></pre>
  
  <p>Resulting figure</p>
  
  <p><a href="https://i.stack.imgur.com/pL1Xf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pL1Xf.png" alt="Resulting Figure"></a></p>