[S] Responses to "Whiskers at the 2.5th and 97.5th centiles"

Richard Dybowski (r.dybowski@umds.ac.uk)
Thu, 05 Nov 1998 19:02:12 +0000


As a number of people have enquired about the responses I got to the
following query

"According to the help file for the boxplot function, default whiskers are
drawn to the nearest value not beyond a standard span from the quartiles,
where the standard span is defined as 1.5*(Inter-Quartile Range). Is there
an argument for this function that will enable the ends of the whiskers to
be located at the 2.5th and 97.5th centiles?"

I have decided to make the answers available to the list. Many thanks to
those who responded.

* From Patrick Burns
-----------------------
You want to use 'bxp' which draws the boxplots given the summary
statistics that you would precompute in a function.
===========================================

* From Frank E Harrell Jr
-----------------------
You might want to check out the panel.bpplot function in the Hmisc
library. It does extended boxplots where you can specify a vector
of quantiles to use for various box widths.
===========================================

* From Robert G Garrett
-----------------------
The function [given below] will plot with either linear or log scaling (on
the y-axis) for the continuous variable, and will subdivided the data into
subsets (along
the x-axis) if required. The latter makes for informative comparisons of
"7-point" graphical data distribution summaries. The function below is set
up to extend the whiskers to the 5th and 95th percentiles in the vector
quant, about line 16. Change the list item 2 to 0.025 and item 6 to 0.975
and you will have what you want I believe.

"rg.bwplot"<-
function(x, ..., log = F, xpos = NA, width, label, by, srt = 0, add = F,
space = 0.25, sort.names = T, xlab
= "", ylab = "")
{
# Doug Nychka, April 28, 1992
# Modified to generate an IDEAS style Box & Whisker plot, Garrett, June 15,
1995
# Example: rg.bwplot(split(Cd.pu,Loc),log=T,xlab="1994 Study Areas",ylab="Cd
(ppb) in Soils")
#
if(is.matrix(x)) data <- data.frame(x)
if(data.class(x) == "numeric")
data <- list(x, ...)
if(is.list(x))
data <- x
if(!missing(by)) data <- rg.cat2list(unlist(data), by) #
# at this point the data should be in list form regardless of how
# the pieces were originally passed
#
quant <- c(0, 0.05, 0.25, 0.5, 0.75, 0.95, 1)
if(log)
logy <- "y"
else {
logy <- " "
}
cols <- length(data)
range.data <- range(as.numeric(unlist(data)), na.rm = T)
if(is.na(xpos)) {
xpos <- 1:cols
}
if(missing(width)) {
width <- min(diff(sort(xpos))) * space
if(cols == 1)
width <- space
}
if(length(width) == 1)
width <- rep(width, cols)
if(!add) {
plot(range(c(xpos - (0.5 * width)/space, xpos + (0.5 *
width)/space)), range.data, log =
logy, type = "n", xaxt = "n", ylab = ylab, xlab =
xlab)
}
for(i in 1:cols) {
temp <- data[[i]]
temp <- temp[!is.na(temp)]
bb <- quantile(temp, quant)
mid <- xpos[i] # i - 0.5
low <- mid - width[i] * 0.5
tiklow <- mid - width[i] * 0.15
hih <- mid + width[i] * 0.5
tikhih <- mid + width[i] * 0.15
if(length(temp) > 5) {
y <- c(bb[2], bb[2], NA, bb[2], bb[3], NA, bb[3],
bb[3], bb[5])
x <- c(tikhih, tiklow, NA, mid, mid, NA, hih, low,
low)
y <- c(y, bb[5], bb[3], bb[4], bb[4], NA, bb[5],
bb[6], bb[6], bb[6])
x <- c(x, hih, hih, hih, low, NA, mid, mid, tikhih,
tiklow)
lines(x, y)
}
else {
y <- c(bb[2], bb[2], NA, bb[2], bb[6], NA, bb[6],
bb[6])
x <- c(tikhih, tiklow, NA, mid, mid, NA, tikhih,
tiklow)
lines(x, y)
points(mid, bb[4], pch = 3)
}
if(bb[1] < bb[2])
points(mid, bb[1], pch = 3)
if(bb[7] > bb[6])
points(mid, bb[7], pch = 3)
}
if(missing(label)) {
if(is.null(names(data)))
label <- format(1:cols)
else label <- names(data)
}
if(length(label) > 7)
srt <- 90
axis(1, xpos, label, tick = F, srt = srt, adj = 1)
}

"rg.cat2list"<-
function(x, a)
{
a <- as.character(a)
label <- unique(a)
out <- as.list(1:length(label))
names(out) <- label
for(k in 1:length(label)) {
out[[k]] <- x[label[k] == a]
}
out
}

===========================================

-------------------------------
Richard Dybowski PhD
Research Fellow (Data Analysis)
King's College London
Intensive Care Unit (Division of Medicine)
St Thomas' Hospital
Lambeth Palace Road
London SE1 7EH
UK

Tel (office): +44 (0)171 928 9292 ext 3245
Tel (mobile): 0976 250092
Fax: +44 (0)171 928 0730
E-mail: r.dybowski@umds.ac.uk
Web site: http://www.umds.ac.uk/microbio/richard/richard.htm
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news