Visualizing small-scale paired data – combining boxplots, stripcharts, and confidence-intervals in R

Sometimes when working with small paired data-sets it is nice to see/show all the data in a structured form. For example when looking at pre-post comparisons, connected dots are a natural way to visualize which data-points belong together. In R this can be easily be combined with boxplots expressing the overall distribution of the data.  This also has the advantage of beeing more true to non-normal data that is not correctly represented by means +/- 95%CI. I have not come up against a good tutorial of how to do such a plot (although the right hand plot borrows heavily from this post on the excellent R-mailing list), so in the post you will find the code to generate such a graph in R.

Here comes the code (Update 05.12.2011 without umlauts):

?View Code RSPLUS
#generatingsomedata
pre<-55+rnorm(20)
post<-pre+0.7+rnorm(20)
 
#Settinguptwoscreens
par(mfrow=c(1,2))
 
#FirstGraph
s<-seq(length(pre))
par(bty="l")
boxplot(pre,post,main="Rawdata",xlab="Time",ylab="Measure",names=c("pre","post"),col=c("lightblue","lightgreen"))
stripchart(list(pre,post),vertical=T,pch=16,method="jitter",cex=0.5,add=T)
segments(rep(0.95,length(pre))[s],pre[s],rep(2,length(pre))[s],post[s],col=1,lwd=0.5)
#Secondgraph
#Confidenceintervals eitherparametric (t.test) or non-parametric (wilcox.text)
#res&lt;-t.test(post,prä,paired=T,conf.int=T)
res<-wilcox.test(post,pre,paired=T,conf.int=T)
 
stripchart(post-pre,vertical=T,pch=16,method="jitter",main="Difference",ylab="Difference:Post–Prä",xlab="Median+/-95%CI")
points(1,res$estimate,col="red",pch=16,cex=2)
arrows(1,res$conf.int[1],1,res$conf.int[2],col="red",code=3,lwd=3,angle=90)
abline(h=0,lty=2)#Zero-effectline

2 thoughts on “Visualizing small-scale paired data – combining boxplots, stripcharts, and confidence-intervals in R”

  1. Thanks, this is great! I’ve been trying to make a mixed boxplot and strip chart just like this for a small paired data set. Unfortunately, I can’t create the connecting lines between the paired data points as you show here. I may be missing a bit of code, can you repost that or email me? Many thanks! Here’s what I have from your post – it creates everything except the connecting lines.

    #generating some data
    prä <- 55+rnorm(20)
    post <- prä+0.7+rnorm(20)

    #Setting up two screens
    par(mfrow=c(1,2))

    #First Graph
    s<-seq(length(sam1))
    par(bty=”l”)
    boxplot(prä, post, main=”Raw data”, xlab=”Time”, ylab=”Measure”, names=c(“prä”, “post”), col=c(“lightblue”,”lightgreen”))
    stripchart(list(prä, post), vertical=T, pch=16,method=”jitter”, cex=0.5, add=T)
    segments(rep(0.95, 10)[s], prä[s], rep(2, 10)[s],post[s], col=1, lwd=0.25)
    #Second graph
    #Confidenceintervals either parametric (t.test) or non-parametric (wilcox.text)
    #res<-t.test(post, prä, paired=T, conf.int=T)
    res<-wilcox.test(post, prä, paired=T, conf.int=T)

    stripchart(post-prä, vertical=T, pch=16, method=”jitter”, main=”Difference”, ylab=”Difference: Post – Prä”, xlab=”Median +/- 95% CI “)
    points(1, res$estimate, col=”red”, pch=16, cex=2)
    arrows(1, res$conf.int[1], 1, res$conf.int[2], col=”red”, code=3, lwd=3, angle=90)
    abline(h=0, lty=2)#Zero-effect line

    1. Hi TM,

      I have updated the code. There was an error in the definition of s and thus the segment- function could not work correclty. I also got rid of the umlauts.

      Cheers
      Gerrit

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>