[R] Boxplot in R

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Mon Jul 11 03:54:28 CEST 2005


1) The boxplot in R does the 25%, 50% and 75% mark as you want
2) Check out the range argument in boxplot. I think you can redefine
your 5% and 95% quantile in terms of IQR for symmetric distribution and
hence use this feature.

However I find it easier to calculate these numbers manually and feed it
into bxp() function. Here is one such function (with lots of room for
improvement).


matlab.boxplot <- function(m){
  m  <- as.matrix(m)
  bp <- boxplot(data.frame(m), plot=FALSE)
  
  bp$stats <- apply( m, 2, function(x) 
                   quantile(x, c(0.05,0.25, 0.5, 0.75, 0.95), na.rm=T) )
  
  tmp <- apply( m, 2, function(x){
    under <- x[ which( x < quantile(x, 0.05, na.rm=T) ) ]
    over  <- x[ which( x > quantile(x, 0.95, na.rm=T) ) ]
    return( c(under, over) )
  })   # always a matrix in this case
  bp$out   <- c(tmp)
  
  bp$group <- rep(1:ncol(tmp), each=nrow(tmp))
  
  bxp(bp)
}


Some usage examples : 

 matlab.boxplot( rnorm(50) )    # a vector

 my.mat <- matrix( rnorm(300), nc=3 )
 matlab.boxplot( my.mat )       # a matrix


Regards, Adai




On Sun, 2005-07-10 at 18:10 -0500, Larry Xie wrote:
> I am trying to draw a plot like Matlab does: 
> 
> The upper extreme whisker represents 95% of the data;
> The upper hinge represents 75% of the data;
> The median represents 50% of the data;
> The lower hinge represents 25% of the data;
> The lower extreme whisker represents 5% of the data.
> 
> It looks like:
> 
>   ---         95%
>    |
>    |
> -------       75%
> |     |
> |-----|       50%
> |     |
> |     |
> -------       25%
>    |
>   ---         5%
> 
> Anyone can give me some hints as to how to draw a boxplot like that?
> What function does it? I tried boxplot() but couldn't figure it out.
> If it's boxplot(), what arguments should I pass to the function? Thank
> you for your help. I'd appreciate it.
> 
> Larry
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list