[R] Center labels on a boxplot

Andy Bunn abunn at montana.edu
Wed Mar 10 17:34:54 CET 2004


Suppose that I have data on three species for a variable and datasets
from two time periods. I want to make a boxplot of the first dataset and
then add the second using 'at = ' and 'add = T' as in the example for
'boxplot.'

Since the boxes are paired by species, I want to do is have the x labels
be centered between the boxes. I'm doing this now with mtext and
entering spaces to make them center. There must be a better way.

Much thanks, Andy

   #######################
   myDF1 <- data.frame(spp1 = c(0,0,0,0,1,0,1,1,0,0,0,1), 
                        spp2 = c(1,0,1,1,0,0,0,0,1,0,0,0),
                        spp3 = c(0,1,0,0,0,1,0,0,0,1,1,0),
                        aVar = runif(12, 1, 10) + 1:12)

    myDF2 <- data.frame(spp1 = c(1,1,0,0,0,0,0,0,1,0,0,1), 
                        spp2 = c(0,0,1,1,0,0,0,1,0,0,1,0),
                        spp3 = c(0,0,0,0,1,1,1,0,0,1,0,0),
                        aVar = runif(12, 1, 10) + 1:12)

    boxplot(aVar ~ spp1 + spp2 + spp3, data = myDF1, boxwex = 0.25, at =
1:3 - 0.2,
                       names = c("","",""), 
                       ylab = "A Varirable of Extreme Interest",
                       ylim = c(0,24))

    boxplot(aVar ~ spp1 + spp2 + spp3, data = myDF2, boxwex = 0.25, at =
1:3 + 0.2,
                       names = c("","",""), add = T, col="gray")
 
    legend(0.5, 24, c("Time 1", "Time 2"),
           fill = c("white", "gray"))    
 
    
   mtext("SPP1, SPP2, SPP3",.....)
   #########################




More information about the R-help mailing list