# Boxplot for several distributions?

I need to draw 20 distributions in a single graph in R, and it doesn't look good (cluttered) to me with regular boxplot (20 boxes) even with boxwex=0.3. Could you suggest me how can I plot a kind of boxplot in R for the 20 distributions, with dots for median and just a line instead of box, like the one below. Please also suggest me if there is any R method that produces nice boxplots, specifically if you want to show several distributions in a single graph.

 -----0----


(This is really a comment, but because it requires an illustration it has to be posted as a reply.)

Ed Tufte redesigned the boxplot in his Visual Display of Quantitative Information (p. 125, First Edition 1983) precisely to enable "informal, exploratory data analysis, where the research worker's time should be devoted to matters other than drawing lines." I have (in a perfectly natural manner) extended his redesign to accommodate drawing outliers in this example showing 70 parallel boxplots:

I can think of several ways to improve this further, but it's characteristic of what one might produce in the heat of exploring a complex dataset: we are content to make visualizations that let us see the data; good presentation can come later.

Compare this to a conventional rendition of the same data:

Tufte presents several other redesigns based on his principle of "maximizing the data ink ratio." Their value lies in illustrating how this principle can help us design effective exploratory graphics. As you can see, the mechanics of plotting them amounts to finding any graphics platform in which you can draw point markers and lines.

• Could you help in drawing the top graph in R? – samarasa Aug 5 '11 at 21:43
• @kkp Here's a rough draft. Nice response (+1). – chl Aug 5 '11 at 23:29
• And here are further possibilities in R -- found on SO: Functions available for Tufte boxplots in R?. – chl Dec 2 '11 at 14:48
• @chl Thank you for the link. For the record, it includes working R code for producing these redesigned boxplots. Interestingly, that question was posted just three days after this one... – whuber Dec 2 '11 at 14:56
• @naught Interesting observations. One potential use of such boxplots is a variant of Tukey's "wandering schematic plot" in which a (large) scatterplot is sliced along the x-coordinate and the y-values are summarized by a boxplot in each bin. Such a procedure can easily generate 70 or more side-by-side boxplots. Applications include almost any multidimensional data: for instance, the x-coordinate might represent a soil depth sampled every centimeter and the y-coordinate might represent data obtained at multiple locations. – whuber Nov 17 '12 at 22:54

# Beanplots

Possibly the coolest plots ever, these are basically a small-multiples implementation of violin plots. Violin plots have a massive advantage over boxplots: they can show a lot more detail for distributions that aren't normal (e.g. they can show bi-modal distributions really well). Because they're usually based on Gaussian smoothing (or similar), they won't work really well for distributions with high end points (like exponential distributions), but then, neither will boxplots.

Beanplots can be achieved very easily in R - just install the beanplot package:

library(beanplot)

# Sampling code from Greg Snow's answer:
my.dat <- lapply( 1:20, function(x) rnorm(x+10, sample( 10, 1), sample(3,1) ) )

beanplot(my.dat)


The beanplot function has tons of options, so you can customise it to your heart's desire. There's also a way to do beanplots in ggplot2 (need the latest version):

library(ggplot2)

my.dat <- lapply(1:20, function(x) rnorm(x+10, sample(10, 1), sample(3,1)))
my.df <- melt(my.dat)
ggplot(my.df, aes(x=L1, y=value, group=L1)) + geom_violin(trim=FALSE) +
geom_segment(aes(x=L1-0.1, xend=L1+0.1, y=value, yend=value), colour='white')


Here is some sample R code for a couple of ways to do it, you will probably want to expand on this (include labels etc.) and maybe turn it ito a function:

my.dat <- lapply( 1:20, function(x) rnorm(x+10, sample( 10, 1), sample(3,1) ) )

tmp <- boxplot(my.dat, plot=FALSE, range=0)

# box and median only
plot( range(tmp$stats), c(1,length(my.dat)), xlab='', ylab='', type='n' ) segments( tmp$stats[2,], seq_along(my.dat), tmp$stats[4,] ) points( tmp$stats[3,], seq_along(my.dat) )

# wiskers and implied box
plot( range(tmp$stats), c(1,length(my.dat)), xlab='', ylab='', type='n' ) segments( tmp$stats[1,], seq_along(my.dat), tmp$stats[2,] ) segments( tmp$stats[4,], seq_along(my.dat), tmp$stats[5,] ) points( tmp$stats[3,], seq_along(my.dat) )