Skip to content

Grouped Boxplot

3 messages · soeren.vogel at eawag.ch, David Winsemius, Richard M. Heiberger

#
Pls forgive me heavy-handed data generation -- newby ;-)

### start ###
# example data
g <- rep.int(c("A", "B", "C", "D"), 125)
t <- rnorm(5000)
a <- sample(t, 500, replace=TRUE)
b <- sample(t, 500, replace=TRUE)

# what I actually want to have:
boxplot(a | b ~ g)

# but that does obviously not produce what I want, instead
i <- data.frame(g, a, rep("one", length(g)))
j <- data.frame(g, b, rep("two", length(g)))
names(i) <- c("Group", "Number", "Word")
names(j) <- c("Group", "Number", "Word")
k <- as.data.frame(rbind(i, j))
boxplot(k$Number ~ k$Word * k$Group)
### end ###

Questions: (1) Is there an easier way? (2) How can I get additional  
space between the A:D groups?

Thank you

S?ren
#
Q1:
See if this seems any better. I took the liberty of reconstruction  
your initial example in a longer dataframe:

dta <- data.frame(val = sample(t,1000), g = gl(4, 250, labels=c("A",  
"B", "C", "D")) , G2 = gl(2,1, labels=c("XX", "YY")))

#arguments to data.frame are recycled so one does not need to make the  
gl call with a
# length of 500, in fact , that only confuses things (or at least it  
does for me).

table(dta$G2,dta$g)

#       A   B   C   D
#  XX 125 125 125 125
#  YY 125 125 125 125

  boxplot( val ~ G2 + g, data=dta)

Q2:

?boxplot  #especially parameter at=

boxplot( val ~ g + G2, data=dta, at = 0.8*c(1,2,3,4,6,7,8,9),  
boxwex=0.4)
#
## you may need to
##     install.packages("HH")
library(HH)

tmp <- data.frame(y=rnorm(500),
                  g=rep.int(c("A", "B", "C", "D"), 125),
                  a=factor(rbinom(500, 1, .5)))
bwplot(y ~ g | a, data=tmp)
bwplot(y ~ a | g, data=tmp)

tmp$ga <- with(tmp, interaction(a, g))
position(tmp$ga) <- c(1.1, 1.9, 3.1, 3.9, 5.1, 5.9, 7.1, 7.9)
bwplot(y ~ ga, data=tmp,
       panel=panel.bwplot.intermediate.hh,
       col=c(1,1,2,2,3,3,4,4),
       main="This is what you asked for")

## I normally want this in context
interaction2wt(y ~ a + g, data=tmp)

interaction2wt(y ~ a + g, data=tmp, simple=TRUE)


position(tmp$a) <- c(1.5, 3.5)
interaction2wt(y ~ a + g, data=tmp, simple=TRUE)
interaction2wt(y ~ a + g, data=tmp,
               simple=TRUE, simple.scale=list(a=.2, g=.4),
               sub="upper-right panel is what you asked for")