Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ? :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
383 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...