[R] Boxplots

Peter Ehlers ehlers at ucalgary.ca
Sat Jan 9 19:35:27 CET 2010


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:
> I have a data set with four columns and need to make boxplots from them.
>  Data is as follows:
>  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
> 
> This is simply consumption data with Total being the actual measured
> consumption and cons_hat being the predicted.  I need a boxplot of tank "a"
> (consisting of a4,a5,a6) "Total" vs. "cons_hat".
> 
> boxplot(Total~tank) of course creates a boxplot with only Total by tank, but
> how do I put the other column in this boxplot?
> 
> Thanks ahead of time.
> 
> keith
> 

-- 
Peter Ehlers
University of Calgary
403.202.3921



More information about the R-help mailing list