[R] boxplot(): formating median in another linethickness?

Marc Schwartz mschwartz at medanalytics.com
Sun Jul 21 17:00:57 CEST 2002

Stephan Holl wrote: 
> Dear guRus, 
> I want to do some boxplots, but the median should appear in another 
> line-thickness and another color. 
> I do not know, what to do in order to realise my wishes. 
> Sorry for that question, but I am new to R. 

There are a couple of approaches:

1. As Uwe has pointed out, you could modify the code for bxp().


2. When you call the boxplot() function, call it with the following

> P <- boxplot("...")

Where the "..." is your specific set of arguments. This assigns the
results of the boxplot call to "P", which is a list of values that
contains plotting summary information for your data. You can use
something other than P of course if you wish.

Type in "P" at the command line (without the quotes) so you can see the
contents of the list. See "Value" in the boxplot help file text
(?boxplot) for a description of the contents of the list.

After plotting your data, use one of the two following calls depending
upon whether you are plotting your graphic vertically or horizontally:


segments(P$stats[3, ], 1:length(P$n)  - 0.4, P$stats[3, ], 1:length(P$n)
+ 0.4, lwd = 3, col = "YourColor")


segments(1:length(P$n)  - 0.4, P$stats[3, ], 1:length(P$n) + 0.4,
P$stats[3, ], lwd = 3, col = "YourColor")

The above calls will need to be modified depending upon whether you have
modified the width, boxwex or varwidth settings and if you are drawing
notches.  The "0.4" values that I use above are for the default "width"
of 1, a default "boxwex" of 0.8, a default varwidth = FALSE and with no
notches being drawn. Thus, the 0.4 is the result of (1 * 0.8) / 2, which
is half the default box width.

If you are drawing notches, the default notch width adjustment is 0.5,
so further multiply the 0.4 I have above by 0.5, so it becomes 0.2.

In R, the box centers are integer values equal to the number of groups
in your data.  Thus the "1:length(P$n)" values define the box centers,
either on the x or y axis depending upon the orientation of your plot.
This presumes that you have not modified the default values with the "at
= " argument.

The P$Stats[3, ] data is the row vector of median values, one for each
group from the list.

The result of the segments call then draws a series of median lines with
the line width that your define by the "lwd =" parameter and the color
that you define with the "col = " parameter.

Which approach you take depends upon how complex you are making the
boxplot.  If you are doing a straight-forward plot, then option 2 I
think would be the easiest and you can use the segments calls as I have
them above.

If you are doing some more complex width or other location adjustments,
then the first option suggested by Uwe would be easier, since the
required code for making the various adjustments is already done for



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