[R] bwplot change whiskers position to percentile 5 and P95

David Winsemius dwinsemius at comcast.net
Wed Oct 13 17:13:56 CEST 2010


On Oct 13, 2010, at 10:05 AM, Christophe Bouffioux wrote:

> Dear R-community,
>
> Using bwplot, how can I put the whiskers at percentile 5 and  
> percentile 95,
> in place of the default position coef=1.5??
>
> Using panel=panel.bwstrip, whiskerpos=0.05, from the package  
> agsemisc gives
> satisfaction, but changes the appearance of my boxplot and works  
> with an old
> version of R, what I don’t want, and I didn’t find the option in
> box.umbrella parameters

Nope, you won't find it even if you search harder, but you do have a  
lattice path forward. Just as base function boxplot() does the  
calculations and then plots with bxp(), by default panel.bwplot sends  
the data to boxplot.stats, but panel.bwplot also allows you to specify  
an alternate function that returns plotting parameters differently as  
long as those conforms to the requirements for structure. You can look  
at boxplot.stats (it's not that big)  and then construct an  
alternative. The line you would need to alter would be the one  
starting with:  stats<-stats::fivenum(...), since you are changing the  
values returned by fivenum(). You might get away with just changing  
stats[1] and stats[5] to your revised specifications, although it has  
occurred to me that you might get some of those "out" dots inside your  
whiskers. (Fixing that would not be too hard once you are inside  
boxplot.stats().

Seemed to work for me with your data (at least the extent of plotting  
a nice 3 x 2 panel display. All I did was redefine an nboxplot.stats  
by inserting this line after the line cited above:

stats[c(1,5)]<- quantile(x, probs=c(0.05, 0.95))

and then added an argument  ..., stats=nboxplot.stats)  inside your  
panel.bwplot.

-- 
David.

> Many thanks
> Christophe
>
> Here is the code:
>
> library(lattice)
> ex <- data.frame(v1 = log(abs(rt(180, 3)) + 1),
>                 v2 = rep(c("2007", "2006", "2005"), 60),
>                 z  = rep(c("a", "b", "c", "d", "e", "f"), e = 30))
>
> ex2 <- data.frame(v1b = log(abs(rt(18, 3)) + 1),
>                 v2 = rep(c("2007", "2006", "2005"), 6),
>                 z  = rep(c("a", "b", "c", "d", "e", "f"), e = 3))
> ex3 <- merge(ex, ex2, by=c("v2","z"))
> D2007 <- ex3[ex3$z=="d" & ex3$v2==2007, ]
> D2006 <- ex3[ex3$z=="d" & ex3$v2==2006, ]
> C2007 <- ex3[ex3$z=="c" & ex3$v2==2007, ]


> quantile(D2007$v1, probs = c(0.05, 0.95))
> quantile(D2006$v1, probs = c(0.05, 0.95))
> quantile(C2007$v1, probs = c(0.05, 0.95))
>
> bwplot(v2 ~ v1 | z, data = ex3, layout=c(3,2), X = ex3$v1b,
> pch = "|",
> par.settings = list(
> plot.symbol = list(alpha = 1, col = "transparent",cex = 1,pch = 20)),
> panel = function(x, y, ..., X, subscripts){
> panel.grid(v = -1, h = 0)
> panel.bwplot(x, y, ..., subscripts = subscripts)
> X <- X[subscripts]
> xmax =max(x)
> X <- tapply(X, y, unique)
> Y <- tapply(y, y, unique)
> tg <- table(y)
> panel.points(X, Y, cex=3, pch ="|" , col = "red")
> #vcount <- tapply(v1, v2, length)
> panel.text((xmax-0.2), (Y-0.15), labels = paste("N=", tg))
> })
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list