Add error bars to show standard deviation on a plot in R

John Garreth picture John Garreth · Feb 25, 2013 · Viewed 120k times · Source

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?

Answer

juba picture juba · Feb 25, 2013

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