[R] error bars on barplots

Uli Flenker; Raum 704 uli at biochem.dshs-koeln.de
Thu Aug 26 20:35:20 CEST 1999


Huuuuuuuuh,
   more experienced R-users will scream and curse ...
... but it works.

I wrote this stuff a long time ago. It's quick and dirty, and I never
improved it, as I don't need it very frequently. 

Expects one vector/matrix for the "means" and one for the errors. It
shouldn't be a big deal to write a "real" thing using arrows().  The trick
is to look what barplot() does to calculate the x-values.  Look at
"xval<-cumsum(...)" in my example.

Hope it helps for the moment ... .

############################################### 

err.bp<-function(daten,error,two.side=F){
   
   if(!is.numeric(daten))
     stop("All arguments must be numeric")
   
   if(is.vector(daten)){
      xval<-(cumsum(c(0.7,rep(1.2,length(daten)-1))))
   }else{
      if (is.matrix(daten)){
         xval<-cumsum(array(c(1,rep(0,dim(daten)[1]-1)),

dim=c(1,length(daten))))+0:(length(daten)-1)+.5
         
      }else{
         stop("First argument must either be a vector or a matrix")
      }
   }

   MW<-0.25*(max(xval)/length(xval))

   ERR1<-daten+error
   ERR2<-daten-error

   for(i in 1:length(daten)){
      segments(xval[i],daten[i],xval[i],ERR1[i])
      segments(xval[i]-MW,ERR1[i],xval[i]+MW,ERR1[i])
      
      if(two.side){segments(xval[i],daten[i],xval[i],ERR2[i])
                   segments(xval[i]-MW,ERR2[i],xval[i]+MW,ERR2[i])
                }
	   }
}


        Uli Flenker
        Institute of Biochemistry
        German Sports University Cologne
        Carl-Diem-Weg 6

        50933 Cologne / Germany

        Phone 0049/0221/4982-493


On Thu, 26 Aug 1999, KAREN KOTSCHY wrote:

> Hello again
> 
> I'm trying to put error bars onto a barplot. I've tried something that 
> Bill Simpson suggested a while ago, ie:
> 
> x<-c(1,2,3,4,5)
> y<-c(1.1, 2.3, 3.0, 3.9, 5.1)
> ucl<-c(1.3, 2.4, 3.5, 4.1, 5.3)
> lcl<-c(.9, 1.8, 2.7, 3.8, 5.0)
> plot(x,y, ylim=range(c(lcl,ucl)))
> arrows(x,ucl,x,lcl,length=.05,angle=90,code=3) 
> #or
> segments(x,ucl,x,lcl)
> 
> but I can't get it to work on a barplot. Any ideas?
> Thanks!
> Karen Kotschy
> Centre for Water in the Environment
> University of the Witwatersrand
> Johannesburg
> 
> Tel: 011 716-2218
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list