Re: [R] Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...

From: Uwe Ligges <>
Date: Mon, 18 Apr 2011 10:16:22 +0200

On 17.04.2011 13:08, Paul Raftery wrote:
> Hi all,
> I'm just getting started with R and I would appreciate some help. I'm having
> trouble creating a boxplot with whiskers at the 95th and 5th percentiles
> instead of at 1.5 * IQR. I have read the relevant documentation, and checked
> existing mails on this topic. I found a small modification that should work
> : and tried
> to implement it.
> Basically, it says to replace boxplot.stats with:
> myboxplot.stats<- function (x, coef = NULL, do.conf = TRUE, do.out =
> {
> nna<- !
> n<- sum(nna)
> stats<- quantile(x, c(.05,.25,.5,.75,.95), na.rm = TRUE)
> iqr<- diff(stats[c(2, 4)])
> out<- x< stats[1] | x> stats[5]
> conf<- if (do.conf)
> stats[3] + c(-1.58, 1.58) * diff(stats[c(2, 4)])/sqrt(n)
> list(stats = stats, n = n, conf = conf, out = x[out& nna])
> }
> I entered the new function, and used fix(boxplot.default) to modify
> boxplot.default so that it references myboxplot.stats instead of the
> original boxplot.stats function.
> If I now type boxplot.default, I can see that the code has been modified as
> expected. However, I get the exact same result as before when I create a
> boxplot - it shows the whiskers at 1.5 * IQR. You can test this out by
> creating a boxplot from the iris dataset supplied with R using
> boxplot(iris$Sepal.Length ~ iris$Species). You see that the boxplot is the
> same before and after the fix. Does anybody know why this occurs, and how I
> can get around this issue?

Frank answered how to solve it using his functions. In order to do it your way, you need to change the boxplot.default function within its NAMESPACE, e.g. using fixInNamespace.

Otherwise you could also just call

   z <- boxplot(....)
then modify the relevant parts of the object z and plot it using



> Thanks, mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 18 Apr 2011 - 08:18:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 09:40:31 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive