[R] Center labels on a boxplot
Marc Schwartz
MSchwartz at medanalytics.com
Wed Mar 10 20:39:17 CET 2004
On Wed, 2004-03-10 at 10:34, Andy Bunn wrote:
> 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",.....)
> #########################
Andy,
Your boxplots are at (1:3 - 0.2) and (1:3 + 0.2). Thus, the centers of
the pairs are at 1:3.
You can use:
mtext(side = 1, text = c("SPP1", "SPP2", "SPP3"), at = 1:3, line = 2)
To place the pair labels in the centers along the x axis.
HTH,
Marc Schwartz
More information about the R-help
mailing list