Wednesday, July 25, 2012

Quick and dirty way to plot error bars in R

If you have data that looks as follows, a one line command can draw error bars to the plot.
-x---T-----y-------------error-----
p5 315 2.513402 0.5711628                                                      
p5 330 2.173868 0.4711594                                                      
p5 345 2.132954 0.634045                                                       
p5 360 2.092686 0.4851832                                                      
p5 375 2.966971 0.9165328                                                      
p5 390 2.214886 0.4288291

r<-read.table("data.txt")
plot(r[,2],r[,3],ylim=c(0,4))
lines(r[,2],r[,3])
for(i in 1:length(r[,1])) {segments(r[i,2],r[i,3]+r[i,4],r[i,2],r[i,3]-r[i,4])}

I am drawing a simple line from (x,y+err) to (x,y-err). The result is not as fancy as other efforts but it is enough for my purposes.

No comments:

Post a Comment