Odp: [R] barplots with std-error

Michal Bojanowski bojaniss at poczta.onet.pl
Wed Mar 13 14:10:07 CET 2002


> hi,
>
> i am trying to generate nice barplots with std-errors.
> do i really have to generate the std-errors myself by the segments()
> command ?
>
> thanks for help,
> jan

Hello Jan

Some time ago I wanted to do the same thing. I wrote a function, which works
in the way similar to
lines(). The code is below.

erbars <- function(yl,ym,yh, x=1:length(yl), w=1/2, ...) {
 points(x,ym, pch=22, ...)
 for(i in 1:length(x)) {
  lines(rep(x[i],2), c(yl[i],yh[i]), lty="dashed", ...)
  lines(c(x[i]-w/2,x[i]+w/2), rep(yh[i],2), ...)
  lines(c(x[i]-w/2,x[i]+w/2), rep(yl[i],2), ...)
 }
}

It will draw a sequence of 'errorbars' (see example below) on the active
device. The meaning of arguments:
yl - a vector specyfying values of lower whisker points
ym - a vector specyfying values of middle points
yh - a vector specyfying values of higher whisker points
x - vector of x coordinates of whiskers
w - width of the ticks

With this function you should be able to add standard errors to your
barplot.

Hope this helps,

regards
Michal

EXAMPLE
x <- rbinom(1000,2,0.4)
y <- 10*x+2+rnorm(length(x),0,5)
f <- as.factor(x)
plot.new()
plot.window(c(0,4),c(min(y),max(y)))
axis(1); axis(2)
erbars(tapply(y,f,min),tapply(y,f,mean), tapply(y,f,max), w=0.2)




-- 

Okresl Swoje potrzeby - my znajdziemy oferte za Ciebie!
[ http://oferty.onet.pl ]

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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