[R] more boxplot questions

Mark Myatt mark at myatt.demon.co.uk
Mon Dec 11 11:39:07 CET 2000

Alexandre Fayolle <Alexandre.Fayolle at free.fr> writes:

>I have a couple more questions about boxplots. In the books I've read on
>statistics (I'm not a statistics expert but just a poor engineer trying
>to help his wife with the analysis of the data she gathered for her
>medieval history PhD), I seen two kinds of box plots: one using
>quartiles to determine the height of the rectangles and whiskers which
>is what the standard R boxplot does, from what I read in the doc, and
>another one using  the the variance (rectangle height = 2s, whiskers
>going from +1s to +2s and -1s to -2s). Is there a way to produce such
>boxplot with R ?

Yes, by using the bxp() function on a data structure containing the relevant values:

        # using a standard R dataset

        # standard boxplot

        # get stats for the standard boxplot
        bp.stats <- boxplot.stats(len)

        # have a look at the structure (list) returned by boxplot.stats()

        # we can recreate this structure but with means and SDs ...
        bp.limits <- as.matrix(c(mean(len) - 2 * sd(len),
                                mean(len) - sd(len),
                                mean(len) + sd(len),
                                mean(len) + 2 * sd(len)))
        bp.stats <- list(stats = bp.limits, n = length(len), conf = NULL,
                         out = numeric(0))

        # and plot it ...

This could be rolled up into a function ... in essence you create a structure (list)
that bxp() can use. Watch out for NA values in the mean(), and sd().

I think that there is an error in the R documentation about the bxp() structure ...
$stats should be a matrix not a vector ... but I may have that wrong.

>Second question: I would like to ponderate the plotted variable with
>another variable. To take an example, the grouping factor A is a
>production city, X is a unitary price and Y is a quantity bought from
>city Ai at price Xi. I would like to make a boxplot of the prices
>ponderated by the quantity bought. Since it sounds like a fairly
>standard visualization, I suppose ther's an easy way to do this with R.

This looks like a problem for coplot() if X and Y are continuous and you want scatters
of them by levels of factors. With boxplot() you need to use the 'interaction' term in
the formula you supply to boxplot. With a standard R dataset:

        boxplot(len~interaction(supp, dose), data = ToothGrowth)

You could make it clearer with by adding colour:

        boxplot(len~interaction(supp, dose), data = ToothGrowth, col = c(7,3))

It only works with data.frames (I think).

I hope that helps.


Mark Myatt

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list