Question

I am having problems trying to add the 95% CI (lower/upper) to an existing plot using the values from two columns in a matrix. What would be the best way to use this information to add the error bars?

Here is a sample of my data:

option<-read.table(text="
distance p.move id option   mean lower95%CI upper95%CI
1      close   0.05  1    10% 13.682     11.306     15.768
2      close   0.10  2    10% 10.886      9.336     12.270
3      close   0.15  3    10%  8.402      7.262      9.580
4      close   0.20  4    10%  7.240      6.132      8.350
5      close   0.25  5    10%  6.322      5.288      7.370
6      close   0.30  6    10%  5.850      4.920      6.714
7      close   0.35  7    10%  3.838      3.084      4.648
8      close   0.40  8    10%  3.600      2.936      4.200
9      close   0.45  9    10%  3.380      2.702      4.016
10     close   0.50 10    10%  3.152      2.462      3.720
11     close   0.55 11    10%  2.772      2.214      3.286
12     close   0.60 12    10%  3.072      2.458      3.596
13     close   0.65 13    10%  2.670      2.134      3.212
14     close   0.70 14    10%  2.194      1.724      2.634
15     close   0.75 15    10%  1.980      1.612      2.336
16     close   0.80 16    10%  2.028      1.594      2.466
17     close   0.85 17    10%  1.650      1.294      1.974
18     close   0.90 18    10%  1.916      1.564      2.254",header=T)

option

This is my plot:

plot(option$mean~option$p.move,xlim=c(0,1),type="o",ylim=c(0,20),
xlab="Probability",ylab="% time",col=1,lwd=1.85)

Thanks a lot in advance,

Was it helpful?

Solution

You can just add the extra columns in with lines which is like plot but draws on the existing plot. (see ?lines, ?points).

Also, when you plot with a data frame you can skip all the option$ by feeding option into the dat argument (see ?plot):

# draw original plot
plot(mean ~ p.move, dat=option, xlim=c(0,1), type="o", ylim=c(0,20),
     xlab="Probability",ylab="% time",col=1,lwd=1.85)

# draw extra lines (the '%' in the column names gets converted to '.' by R)
# note you can put your usual `plot` arguments into `lines` like lwd, type etc
# if you want
lines(upper95.CI ~ p.move, option)
lines(lower95.CI ~ p.move, option)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top