Advanced techniques for BoxplotsThis example uses the R included data “iris”

1 Housekeeping

rm(list=ls()) # remove everything currently held in the R memory

Enter or read in your data from a file. In this example we will use the in-built dataset “iris” which gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

see ?iris for more information

2 A basic default boxplot

par(mfrow=c(1,1))
boxplot( Petal.Length ~ Species, data = iris,
        ylab="Petal Length (cm)", xlab="Species")

3 Customising boxplot

Here are some advanced tricks to customising plots.Remove the frame surrounding the boxplot by suppressing the axes and manually specifying them. NB this is straight forward for most plots, but for some reason the boxplot function is encoded differently.

3.1 Comparison of old and new plot without frame - not good for publication

par(mfrow=c(2,1))
boxplot(  Petal.Length ~ Species, data = iris, 
          ylab="Petal Length (cm)", xlab="Species") # old one
boxplot(  Petal.Length ~ Species, data = iris,
          frame.plot=F ) # new one

3.2 Removing of the axes

par(mfrow=c(1,1))
boxplot( Petal.Length ~ Species, data = iris,  axes=F) # axes=F removes axes from the plot

3.3 Adding the L box shape

boxplot(  Petal.Length ~ Species, data = iris,  axes=F)
box(bty="L") # adding L-type box on your plot

3.4 Adding information to the x-axis

boxplot(  Petal.Length ~ Species, data = iris, axes=F)
box(bty="L")
axis(1,at=1:3,c("Iris setosa","Iris versicolor","Iris virginica"),
        font=3, cex.axis=1.2, tcl=0.5) # font =3 shows x-axis font in italic; cex.axis= 1.2 increases font size for 20% compared to the default setting; tlc=0.5 shows ticks inside the box - default is -0.5.

3.5 Adding information to the y-axis

boxplot(  Petal.Length ~ Species, data = iris, ylim = c(0,7), axes=F)
box(bty="L")
axis(1,at=1:3,c("Iris setosa","Iris versicolor","Iris virginica"),
        font=3, cex.axis=1.2, tcl=0.5)
title(xlab="Species", cex.lab=1.4)
axis(2, at=0:7, las=1, tcl=0.5) # las=1 changes direction of the y-axis text
title(ylab="Petal Length (cm)",cex.lab=1.2)

3.6 Adding lightgray colour to the boxplot

boxplot(  Petal.Length ~ Species, data = iris, 
          ylim = c(0,7), axes = F, col = "lightgray")
box(bty="L")
axis(1,at=1:3,c("Iris setosa","Iris versicolor","Iris virginica"),
        font=3, cex.axis=1.2, tcl=0.5)
title(xlab="Species", cex.lab=1.4)
axis(2, at=0:7, las=1, tcl=0.5) # las=1 changes direction of the y-axis text
title(ylab="Petal Length (cm)",cex.lab=1.2)

3.7 Adding colour to the boxplot

boxplot(  Petal.Length ~ Species, data = iris, 
          ylim = c(0,7), axes = F, col = rainbow(10))
box(bty="L")
axis(1,at=1:3,c("Iris setosa","Iris versicolor","Iris virginica"),
        font=3, cex.axis=1.2, tcl=0.5)
title(xlab="Species", cex.lab=1.4)
axis(2, at=0:7, las=1, tcl=0.5) # las=1 changes direction of the y-axis text
title(ylab="Petal Length (cm)",cex.lab=1.2)

4 Saving tiff from your plot

tiff("outfile.tif", compression = "lzw") # produces nice tiff
#tiff(file = "temp.tiff", width = 3200, height = 3200, units = "px", res = 800) # produced large size tiff 
boxplot(  Petal.Length ~ Species, data = iris, 
          ylim=c(0,7), axes=F, col=rainbow(10))
box(bty="L")
axis(1,at=1:3,c("Iris setosa","Iris versicolor","Iris virginica"),
        font=3, cex.axis=1.2, tcl=0.5)
title(xlab="Species", cex.lab=1.4)
axis(2, at=0:7, las=1, tcl=0.5) # las=1 changes direction of the y-axis text
title(ylab="Petal Length (cm)",cex.lab=1.2)