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

Deepayan Sarkar deepayan at stat.wisc.edu
Thu May 5 19:28:36 CEST 2005

```On Wednesday 04 May 2005 10:30, Sander Oom wrote:
> 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.

There's an example in this thread:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/50299.html

There might be useful tools in the Hmisc package as well.

> The individual plots within the lattice graph could look like this:
>
> # Example with confidence intervals and grid
> 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)

This gives me

Error: couldn't find function "barplot2"

Maybe you missed a library() call?

Deepayan

> mtext(side = 1, at = colMeans(mp), line = -2,
>        text = paste("Mean", formatC(colMeans(hh))), col = "red")
> box()
>
> Or like this:
>
> data(state)
> 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",
> "none").
>
> The following test data is still useful:
>
> tmp <- expand.grid(geology = c("Sand","Clay","Silt","Rock"),
>    species =
> c("ArisDiff","BracSera","CynDact","ElioMuti","EragCurS","EragPseu"),
>    dist = seq(1,9,1) )
> tmp\$height <- rnorm(216)
>
> For instance plotting height versus dist by geology.
>
> Any help very welcome!
>
> Cheers,
>
> Sander.
>
> PS Of course the resulting graph will go to the R graph gallery!

```