Skip to content
Prev 302909 / 398503 Next

Fill pattern for Boxplots?

It's not too hard to use rect() to add shading to the boxplots. The boxes
are centered on consecutive integers and the width is +/- .4. The boxplot()
function returns the quartiles of each box. 

set.seed(42)
DF <- data.frame(val=rnorm(150), grp=rep(letters[1:6], 25))
outstat <- boxplot(val~grp, DF)           # save the boxplot stats
ngroups <- length(levels(DF$grp))         # get number of groups
dval <- c(6, 6, 6, 12, 12, 12)            # density vals for each group
aval <- c(0, 30, 60, 90, 120, 150)        # angle vals for each group
rect((1:ngroups)-.4, outstat$stats[2,], (1:ngroups)+.4, outstat$stats[4,], 
     density=dval, angle=aval)            # draw the rectangles

You can also play with lty= and lwd= but that will affect the box border as
well.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352