-2

In R I have the following dataframe:

  Group mean  sd
1     1   21.2  5.202563
2     2   28.4  6.113737
3     3   21.8  2.529822

I would like to create a barplot with the means and the standard deviations as arrows on top of the means like this example: enter image description here

This is the code I have so far:

barCenters <- barplot(height = Ymeans12stdev$mean,main = "Average Time per Group",
                  xlab = "Group", ylab = "Time")

However, I am not succeeding in adding the standard deviation bars. Can anyone solve this ? :)

Afke
  • 899
  • 2
  • 10
  • 21
  • 1
    Possible duplicate of [Grouped barplot in R with error bars](https://stackoverflow.com/questions/29768219/grouped-barplot-in-r-with-error-bars) – nghauran Mar 30 '18 at 14:41

1 Answers1

4

with base R, you can use the function arrows() :

barCenters <- barplot(height = Ymeans12stdev$mean,
                      main = "Average Time per Group", xlab = "Group", ylab = "Time")
arrows(barCenters, Ymeans12stdev$mean-Ymeans12stdev$sd,
       barCenters, Ymeans12stdev$mean+Ymeans12stdev$sd,angle=90,code=3)

the argument angle=90 specifies to draw "flat" arrows (i.e. a horizontal bar on top of a vertical one) and the argument code=3 specifies to draw arrows on both ends of the vertical line. You can add the argument length to increase/reduce the size of the horizontals bars of the arrows.

xraynaud
  • 2,028
  • 19
  • 29