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

From: Sander Oom <slist_at_oomvanlieshout.net>
Date: Fri 06 May 2005 - 16:44:14 EST

I noticed the other thread before, but I was really looking for a dot plot with error bars. This looks better indeed, as you state yourself in that thread.

The package Hmisc gives the solution through Cbind and xYplot! I will prepare an example graph for the gallery.

Thanks again,

Sander.

library(Hmisc)
# Examples of plotting raw data
dfr <- expand.grid(month=1:12, continent=c('Europe','USA'),

```                    sex=c('female','male'))
```
set.seed(1)
dfr <- upData(dfr,
```               y=month/10 + 1*(sex=='female') + 2*(continent=='Europe') +
runif(48,-.15,.15),
lower=y - runif(48,.05,.15),
upper=y + runif(48,.05,.15))

xYplot(Cbind(y,lower,upper) ~ month,subset=sex=='male' & continent=='USA',
data=dfr)
```

xYplot(Cbind(y,lower,upper) ~ month|continent, subset=sex=='male',data=dfr) xYplot(Cbind(y,lower,upper) ~ month|continent, groups=sex, data=dfr); Key() # add ,label.curves=FALSE to suppress use of labcurve to label curves where # farthest apart

Deepayan Sarkar wrote:

```> 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!
```>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>

```
```--
--------------------------------------------
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@oomvanlieshout.net
Web     www.oomvanlieshout.net/sander

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help