Friday, May 14, 2010

boxplot with data points jittered


## CAN BE REPLACED BY qplot in ggplot2 package


qplot(x,y,geom=c("boxplot","jitter"))






## R function that plots boxplot and adds data points (jittered horizontally) 

## it can be improved so that it can take a model formula as argument
## like boxplot does
boxjitter =
function(x,y=NULL,border='gray',jitter=.1,vertical=T,...)
{
  if(!is.null(y))
  {
    boxplot(y~x,border=border,ylim = range(y,na.rm=T),outline=F,...)
    stripchart(y~x,vertical=vertical,method="jitter",jitter=jitter,add=T)
    meanvec = tapply(y,x,mean.na)
    points(1:length(meanvec),meanvec,pch="M",col='blue')
  } else
  {
    x = x[!is.na(x)]
    boxplot(x,border=border,ylim = range(x),outline=F,...)
    stripchart(x,vertical=vertical,method="jitter",jitter=jitter,add=T)
    points(1,mean.na(x),pch="M",col='blue')
  }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

Followers

google analytics