Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

bar chart - Adding standard deviation to barplot() in R

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...