Advanced techniques for BoxplotsThis example uses the R included data “iris”
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
par(mfrow=c(1,1))
boxplot( Petal.Length ~ Species, data = iris,
ylab="Petal Length (cm)", xlab="Species")
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.
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
par(mfrow=c(1,1))
boxplot( Petal.Length ~ Species, data = iris, axes=F) # axes=F removes axes from the plot
boxplot( Petal.Length ~ Species, data = iris, axes=F)
box(bty="L") # adding L-type box on your plot
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.
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)
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)
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)
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)