[R] Help boxplot to add mean, standard error and/or stadard deviation

Jim Lemon jim at bitwrit.com.au
Tue Jan 31 10:30:18 CET 2012


On 01/30/2012 11:56 PM, gianni lavaredo wrote:
> Hey Thanks Jim,
>
> is It possible to plot in the same plot mean, standard error AND
> standard deviation. As in figure 3 of the paper I attached
>
Hi gianni,
Here is a rather rough function that will do more or less what you want. 
You can choose any two measures of dispersion and plug them in as 
ulim/llim (outer) and uinner/linner (inner). The example shows how you 
can get asymmetric dispersion measures. For symmetric measures, you only 
have to pass ulim and uinner. You will probably have to do a little 
refining on the function to get exactly what you want.

box.heresy<-function(x,y,uinner,linner=uinner,ulim,llim=ulim,
  intervals=FALSE,arrow.cap=0.01,pch=22,main="",xlab="",ylab="",
  xaxlab=NA,col="white",...) {

  if(missing(y)) {
   y<-x
   x<-1:length(y)
  }
  if(is.na(xaxlab)) xaxlab<-x
  if(intervals) {
   ulim<-y+ulim
   llim<-y-llim
  }
  xrange<-range(x)
  xspace<-diff(xrange)/10
  boxwidth<-diff(xrange)/(4*length(x))
  plot(x,y,xlim=c(xrange[1]-xspace,xrange[2]+xspace),
   ylim=range(c(llim,ulim)),
   main=main,xlab=xlab,ylab=ylab,type="n",xaxt="n")
  axis(1,at=x,labels=xaxlab)
  dispersion(x,y,ulim,llim,intervals=FALSE,arrow.cap=arrow.cap,...)
  if(intervals) {
   uinner<-y+uinner
   linner<-y-linner
  }
  rect(x-boxwidth,linner,x+boxwidth,uinner,col=col)
  points(x,y,pch=pch)
}

require(plotrix)
y<-runif(5)
ulim<-runif(5)
llim<-runif(5)
uinner<-ulim/2
linner<-llim/2
box.heresy(y,uinner=uinner,linner=linner,ulim=ulim,llim=llim,
  intervals=TRUE,main="The heretical boxplot",
  xlab="Number of observations",ylab="Value")

Have fun.

Jim



More information about the R-help mailing list