[R] indicating significant differences in boxplots

Jim Lemon jim at bitwrit.com.au
Mon Oct 28 20:57:49 CET 2013


On 10/29/2013 01:51 AM, Johannes Radinger wrote:
> Hi,
>
> I'd like to follow up an older discussion on adding significance stars to
> boxplots.
> (
> http://r.789695.n4.nabble.com/indicating-significant-differences-in-boxplots-td862335.html
> )
> As suggested by Jim Lemon, there is following solution for a single boxplot:
>
> boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
>
> #and we'll assume that the second and third boxplots require a star:
> par(xpd=TRUE)
> yrange<-par("usr")[3:4]
> ypos<-yrange[2]+diff(yrange)/40
> segments(2,ypos,3,ypos)
> text(2.5,ypos+diff(yrange)/40,"*",cex=2)
> par(xpd=FALSE)
>
> But what if one wants to plot two boxplots (e.g. with each only two groups
> which are significantly different) and
> add such stars to each of the boxplots. I usually use par(mfrow(1,2)) to
> plot all boxplots in a row.
> How can I also add segments and stars on top of each plot?
>
Hi johannes,
The general strategy above can be applied. Fortunately, boxplots are 
centered on successive integers (i.e. 1,2,...) by default. The 
calculations for ypos yield 1/40th of the vertical span of the plot 
above the top of the plot. Just add the segments and stars after each 
plot, as each boxplot advances to the next frame in your plot matrix.

Jim



More information about the R-help mailing list