32

For each X-value I calculated the average Y-value and the standard deviation (sd) of each Y-value

x  = 1:5
y  = c(1.1, 1.5, 2.9, 3.8, 5.2)
sd = c(0.1, 0.3, 0.2, 0.2, 0.4)

plot (x, y)

How can I use the standard deviation to add error bars to each datapoint of my plot?

epo3
  • 2,991
  • 2
  • 33
  • 60
John Garreth
  • 1,112
  • 2
  • 10
  • 17

5 Answers5

32

A solution with ggplot2 :

qplot(x,y)+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd), width=0.25)

enter image description here

juba
  • 47,631
  • 14
  • 113
  • 118
  • setting geom_errorbar(width = 0) removes caps from both error bar ends. Any idea of how this could be done only on one side of the error bar? Thanks – AJMA May 27 '17 at 05:33
22

In addition to @csgillespie's answer, segments is also vectorised to help with this sort of thing:

plot (x, y, ylim=c(0,6))
segments(x,y-sd,x,y+sd)
epsilon <- 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

enter image description here

thelatemail
  • 91,185
  • 12
  • 128
  • 188
21

You can use arrows:

arrows(x,y-sd,x,y+sd, code=3, length=0.02, angle = 90)
HubertL
  • 19,246
  • 3
  • 32
  • 51
SmartCH
  • 231
  • 2
  • 6
  • 2
    This answer is better than the answers involving `segments`; simpler, and works even when the x axis is on a log scale (which would cause the solutions using `segments` to draw different lengths to the left versus the right). – bhaller Feb 25 '16 at 13:25
  • @SmartCH, how do I compute the interquartiles by row of a matrix and then plot the interquartiles and mean as above, please? – Joke O. Mar 23 '16 at 16:23
20

You can use segments to add the bars in base graphics. Here epsilon controls the line across the top and bottom of the line.

plot (x, y, ylim=c(0, 6))
epsilon = 0.02
for(i in 1:5) {
    up = y[i] + sd[i]
    low = y[i] - sd[i]
    segments(x[i],low , x[i], up)
    segments(x[i]-epsilon, up , x[i]+epsilon, up)
    segments(x[i]-epsilon, low , x[i]+epsilon, low)
}

As @thelatemail points out, I should really have used vectorised function calls:

segments(x, y-sd,x, y+sd)
epsilon = 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)

enter image description here

csgillespie
  • 59,189
  • 14
  • 150
  • 185
18

A Problem with csgillespie solution appears, when You have an logarithmic X axis. The you will have a different length of the small bars on the right an the left side (the epsilon follows the x-values).

You should better use the errbar function from the Hmisc package:

d = data.frame(
  x  = c(1:5)
  , y  = c(1.1, 1.5, 2.9, 3.8, 5.2)
  , sd = c(0.2, 0.3, 0.2, 0.0, 0.4)
)

##install.packages("Hmisc", dependencies=T)
library("Hmisc")

# add error bars (without adjusting yrange)
plot(d$x, d$y, type="n")
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=T, pch=1, cap=.1)
)

# new plot (adjusts Yrange automatically)
with (
  data = d
  , expr = errbar(x, y, y+sd, y-sd, add=F, pch=1, cap=.015, log="x")
)
R_User
  • 10,682
  • 25
  • 79
  • 120