Adding standard deviation to barplot() in R

对着背影说爱祢 提交于 2019-12-13 10:44:16

问题


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:

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


回答1:


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.



来源:https://stackoverflow.com/questions/49576344/adding-standard-deviation-to-barplot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!