[R] Plotting means and confidence intervals by group factor using lattice graphics?

Sander Oom slist at oomvanlieshout.net
Wed May 4 17:30:04 CEST 2005

Dear R graphics gurus,

Another question about lattice graphics. This time I would like to plot 
means and confidence intervals by group factor in a lattice graph. I can 
not find any working lattice examples. Maybe a custom panel function is 
the answer, but that is a bit beyond me for now.

The individual plots within the lattice graph could look like this:

# Example with confidence intervals and grid
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
ci.l <- hh * 0.85
ci.u <- hh * 1.15
mp <- barplot2(hh, beside = TRUE,
         col = c("lightblue", "mistyrose",
                 "lightcyan", "lavender"),
         legend = colnames(VADeaths), ylim = c(0, 100),
         main = "Death Rates in Virginia", font.main = 4,
         sub = "Faked 95 percent error bars", col.sub = mybarcol,
         cex.names = 1.5, plot.ci = TRUE, ci.l = ci.l, ci.u = ci.u,
         plot.grid = TRUE)
mtext(side = 1, at = colMeans(mp), line = -2,
       text = paste("Mean", formatC(colMeans(hh))), col = "red")

Or like this:

plotmeans(state.area ~ state.region)

Both plotmeans and barplot2 give interesting options such as printing of 
nobs, among other things. In case of a barplot, there should be an 
option to plot the confidence intervals in one direction only (up) as to 
avoid interference with any black and white shading. The plotMeans 
function provides a useful option error.bars ("se", "sd", "conf.int", 

The following test data is still useful:

tmp <- expand.grid(geology = c("Sand","Clay","Silt","Rock"),
   species = 
   dist = seq(1,9,1) )
tmp$height <- rnorm(216)

For instance plotting height versus dist by geology.

Any help very welcome!



PS Of course the resulting graph will go to the R graph gallery!

Dr. Sander P. Oom
Animal, Plant and Environmental Sciences,
University of the Witwatersrand
Private Bag 3, Wits 2050, South Africa
Tel (work)      +27 (0)11 717 64 04
Tel (home)      +27 (0)18 297 44 51
Fax             +27 (0)18 299 24 64
Email   sander at oomvanlieshout.net
Web     www.oomvanlieshout.net/sander

More information about the R-help mailing list