问题
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