Sander Oom
2005-May-04 15:30 UTC
[R] Plotting means and confidence intervals by group factor using lattice graphics?
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") 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! -- -------------------------------------------- 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
Deepayan Sarkar
2005-May-05 17:28 UTC
[R] Plotting means and confidence intervals by group factor using lattice graphics?
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 > 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)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!