[R] Questions about bwplot

Klaus Nordhausen klausch at gmx.de
Mon May 21 14:39:49 CEST 2007


Dear R-experts,

I have some questions about boxplots with lattice.
My data is similar as in the example below, I have two factors
(Goodness of Fit and Algorithms) and data values but in each panels the scales are quite different, therefore the normal boxplots produced by

set.seed(1)
GOF <- factor(rep(c("GOF1","GOF2","GOF3"),each=40))
Alg <- rep(factor(rep(c("A1","A2","A3","R1"),each=10)),3)
Value <- c(runif(40),rnorm(40),rnorm(30,10,3),rnorm(10,20,3))
test.data <- data.frame(Alg=Alg,GOF=GOF,Value=Value)

library(lattice)
bwplot(Value ~ Alg | GOF, data = test.data, as.table=T, layout=c(1,3))

are not very informative.

Then I used

bwplot(Value ~ Alg | GOF, data = test.data, scale=list(relation="free"), as.table=T, layout=c(1,3))

from which my first question arises:

Is it possible to have no vertical space between the panels though they have different y-scales when using the
argument scale=list(relation="free")?

Then I would like to modify the boxplot - actually I'm not interested so much in the boxes of factor R1 (=random guess)
but would rather have a horizontal line in each panel at the median of R1 for the given panel, and only the boxes for levels
A1, A2 and A3 - and the horizontal line I would like only if it would fall in the plotting area when only plotting the boxes
for the levels A1, A2 and A3 (this means in the third panel the line should not be there).

My attempt 

test.data$ind <- ifelse(Alg=="R1",1,0)
my.boxpanel <- function(x, y, subscripts, groups, ...) {
                orig <- groups[subscripts] == 0
                panel.bwplot(x[orig], y[orig], ...)
                panel.abline(h=median(y[!orig]))
                  }
bwplot(Value ~ Alg | GOF, data = test.data, scale=list(relation="free"),
        as.table=T, groups=ind, layout=c(1,3),panel=my.boxpanel, 		        drop.unused.levels=T,
        xlim=c("A1","A2","A3"),
        ylim=list(c(0,1),c(-2,2),c(0,17)))

requires that I know the range of the yaxis and at the xaxis leaves space for the box of R1. Can that be avoided?

Then my last question - I would like to emphasise the best factor=algorithm in each panel by coloring its box.
However "best" means in the first two panels lowest median and in the last panel highest median. Can this be done?
I managed it only when in all panels the lowest median box should be filled with color.

trellis.device(color = FALSE)
my.boxpanel2 <- function(x, y, subscripts, groups, ...) {
                orig <- groups[subscripts] == 0
                medians<-tapply(y[orig],x[orig],median)
                medians2<-medians[!is.na(medians)]
                cols<-grey((1-as.numeric(medians2==min(medians2))/3))
                z<-x[orig]
                levels(z)<-c("A1","A2","A3",NA)
                w<-y[orig]
                panel.bwplot(z, w, fill=cols, ...)
                panel.abline(h=median(y[!orig]))
                  }  

bwplot(Value ~ Alg | GOF, data = test.data, scale=list(relation="free"),
        as.table=T, groups=ind, layout=c(1,3),panel=my.boxpanel2, drop.unused.levels=T, xlim=c("A1","A2","A3"),
        ylim=list(c(0,1),c(-2,2),c(0,17)))

It would be great if you could help me with this plot!

Thanks in advance,

Klaus
--



More information about the R-help mailing list