Skip to content

Boxplots

2 messages · Marlin Keith Cox, Peter Ehlers

#
I'm guessing that you want side-by-side boxplots.
If that's correct, then try the following.
It stacks the y-values, makes an appropriate grouping
factor and plots with reasonable spacing.

dat <- read.table(textConnection("
  tank Tanks    Total cons_hat
1    a    a4 5.651017     5.59
2    a    a5 5.017499     5.29
3    a    a6 4.894238     4.69
4    c    c4 3.986347     3.40
5    c    c5 4.099442     3.58
6    c    c6 4.150522     3.64
7    h    h4 5.187792     6.32
8    h    h5 6.713422     6.44
9    h    h6 5.168555     5.62"),header=T)
closeAllConnections()

library(reshape) #or you can use stats::reshape() with suitable
                  # modifications
tmp <- melt(dat, id.vars="tank",
            measure.vars=c("Total", "cons_hat"),
            variable_name="Group")
tmp

tmp$grp <- with(tmp, factor(paste(tank, as.numeric(Group), sep=":")))
tmp

boxplot(value ~ grp, data=tmp, boxwex=0.5,
    col=c("red", "blue"),
    at=c(1.2,1.8,3.2,3.8,5.2,5.8))

  -Peter Ehlers
Marlin Keith Cox wrote: