Search code examples
rggplot2arrows

How to get type T arrows in geom_segment?


I need to manually add error bars on a ggplot with geom_crossbar.

I have tried with "geom_arrow" but I could not find how to change the size of the arrow or to change the angle of the arrow to 90 (as I can do with functions arrows in plot). The closest I have got is with "geom_segment", where I could choose the arrow length, but still could not change the arrow angme.

Below is an example of what I would like to get: enter image description here

Here is the code with geom_segment:

xaxis = c(5,6,7,8)
yaxis1 = c(3,3,2,1)
yaxis2 = c(6,5,3,3)
df = data.frame(cbind(xaxis,yaxis1,yaxis2))
ggplot(df) +
  geom_crossbar(aes(ymin=yaxis1, ymax=yaxis2, 
                    x=xaxis, y=yaxis1),
                fill = alpha("black",0.5), fatten=0) +
  geom_segment(mapping=aes(x=xaxis, y=yaxis1-0.4, xend=xaxis, yend=yaxis1+0.4),
               color="black",
               arrow=arrow(length = unit(0.25, "cm"), ends="both"))

Any help appreciated!


Solution

  • In ggplot, you can use geom_errorbar to add error bars to your bargraph and then set the width using the argument width:

    library(ggplot2)
    ggplot(df) +
      geom_crossbar(aes(ymin=yaxis1, ymax=yaxis2, 
                        x=xaxis, y=yaxis1),
                    fill = alpha("black",0.5), fatten=0) +
      geom_errorbar(mapping=aes(x=xaxis, ymin=yaxis1-0.4, ymax=yaxis1+0.4),
                   color="black", width = 0.25)
    

    enter image description here

    Is it what you are looking for ?