0
votes

How do I plot several bootstrap confidence intervals on one plot in R? I created an Excel document with the lower and upper limit of the confidence interval. I want a plot where each year there are two confidence intervals (either line or box will be fine) in two colors to denote Group A or B.

I tried to use the bwplot function, but the code I tried didn't work.

bwplot(Lower+Upper~Year, data=yeargroupboot)

Data:

Year     Upper      Lower   Site  
2001     123        121      A  
2001     115        113      B  
2002     127        124      A  
2002     114        113      B  
2003     145        141      A  
2003     100        99       B  
1

1 Answers

0
votes
  • I couldn't quite get bwplot to work the way you want: bwplot(Lower+Upper~factor(Year)|Site,data=dd) was the closest I got
  • You can use plotCI (from the plotrix or gplots packages) to do this by hand;
  • or use ggplot2 as follows:

Construct data:

dd <- read.table(header=TRUE,text="
   Year     Upper      Lower   Site  
    2001     123        121      A  
    2001     115        113      B  
    2002     127        124      A  
    2002     114        113      B  
    2003     145        141      A  
    2003     100        99       B ")

library("ggplot2"); theme_set(theme_bw())
ggplot(dd)+
   geom_linerange(aes(Year,ymin=Lower,ymax=Upper,colour=Site),
                  position=position_dodge(width=0.25))+
   scale_x_continuous(breaks=2001:2003)