Paul Raftery
2011-Apr-17 11:08 UTC
[R] Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...
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 : stat.ethz.ch/pipermail/r-help/2001-November/016817.html and tried to implement it. Basically, it says to replace boxplot.stats with: myboxplot.stats <- function (x, coef = NULL, do.conf = TRUE, do.out TRUE) { nna <- !is.na(x) 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? Thanks, -- Regards, Paul ====================Contact Details ====================Paul Raftery, BEng(Hons) (Mech), Fulbright Fellow, PhD paulraftery.com <paulraftery.com> Postdoctoral Research Engineer Informatics Research Unit for Sustainable Engineering (IRUSE) iruse.ie Department of Civil Engineering, National University of Ireland, Galway, University Road, Galway, Ireland. Landline: +353 91 49 3086 Mobile: +353 85 124 7947 Skype: praftery [[alternative HTML version deleted]]
Frank Harrell
2011-Apr-17 21:56 UTC
[R] Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...
Try require(Hmisc) ?panel.bpplot This implements extended box plots that can show a variety of quantiles. Frank 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 > : stat.ethz.ch/pipermail/r-help/2001-November/016817.html and > tried > to implement it. > > Basically, it says to replace boxplot.stats with: > > myboxplot.stats <- function (x, coef = NULL, do.conf = TRUE, do.out > TRUE) > { > nna <- !is.na(x) > 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? > > Thanks, > -- > Regards, > Paul > > > ====================> Contact Details > ====================> Paul Raftery, BEng(Hons) (Mech), Fulbright Fellow, PhD > paulraftery.com > <paulraftery.com/> > Postdoctoral Research Engineer > Informatics Research Unit for Sustainable Engineering (IRUSE) > iruse.ie > > Department of Civil Engineering, > National University of Ireland, Galway, > University Road, > Galway, > Ireland. > > Landline: +353 91 49 3086 > Mobile: +353 85 124 7947 > Skype: praftery > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >----- Frank Harrell Department of Biostatistics, Vanderbilt University -- View this message in context: r.789695.n4.nabble.com/Box-plot-with-5th-and-95th-percentiles-instead-of-1-5-IQR-problems-implementing-an-existing-solution-tp3456123p3456187.html Sent from the R help mailing list archive at Nabble.com.
Uwe Ligges
2011-Apr-18 08:16 UTC
[R] Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...
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 > : stat.ethz.ch/pipermail/r-help/2001-November/016817.html and tried > to implement it. > > Basically, it says to replace boxplot.stats with: > > myboxplot.stats<- function (x, coef = NULL, do.conf = TRUE, do.out > TRUE) > { > nna<- !is.na(x) > 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 bxp(z) afterwards. Best, Uwe> > Thanks,
Olaf
2013-Jul-17 09:14 UTC
[R] Box plot with 5th and 95th percentiles instead of 1.5 * IQR: problems implementing an existing solution...
Hello, you may just first change the boxplot.stats directly: boxplot.stats <- function (x, coef = NULL, do.conf = TRUE, do.out TRUE) { nna <- !is.na(x) 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]) } then use it plotting your data. And after that change them back : boxplot.stats <- function (x, coef = 1.5, do.conf = TRUE, do.out = TRUE) { if (coef < 0) stop("'coef' must not be negative") nna <- !is.na(x) n <- sum(nna) stats <- stats::fivenum(x, na.rm = TRUE) iqr <- diff(stats[c(2, 4)]) if (coef == 0) do.out <- FALSE else { out <- if (!is.na(iqr)) { x < (stats[2L] - coef * iqr) | x > (stats[4L] + coef * iqr) } else !is.finite(x) if (any(out[nna], na.rm = TRUE)) stats[c(1, 5)] <- range(x[!out], na.rm = TRUE) } conf <- if (do.conf) stats[3L] + c(-1.58, 1.58) * iqr/sqrt(n) list(stats = stats, n = n, conf = conf, out = if (do.out) x[out & nna] else numeric()) } it works fine for me. -- View this message in context: r.789695.n4.nabble.com/Box-plot-with-5th-and-95th-percentiles-instead-of-1-5-IQR-problems-implementing-an-existing-solution-tp3456123p4671739.html Sent from the R help mailing list archive at Nabble.com.
Apparently Analagous Threads
- Modifying whiskers in boxplots?
- How to pass value to an argument in a function which is an argument to the main function
- Custom Plot - means, SD & 5th-95th% (Plotmeans or Boxplot)?
- boxplot, notches, etc.
- unexpected behavior of boxplot(x, notch=TRUE, log="y")