8rino-Luca Pantani
2004-Nov-05 11:05 UTC
[R] I: a coloured band within each panel of a lattice bwplot
Hi all, I would like to add to each panel of a bwplot a coloured central band, centered on the mean of the values, being its width +- 2% of the mean itself. I know how to add lines, i.e. something like bwplot(X ~ Y|FACTOR data=my.df, panel= function(x, y){ panel.bwplot(x, y) panel.abline(v = mean(x, na.rm = T) - mean(x, na.rm = T) * 0.02 panel.abline(v = mean(x, na.rm = T) + mean(x, na.rm = T) * 0.02 }) but I cannot figure out how to shade the region included between the two lines. Is it possible, or I'm pretending too much? Thanks to all. Ottorino-Luca Pantani, Universit?? di Firenze Dip. Scienza del Suolo e Nutrizione della Pianta
Deepayan Sarkar
2004-Nov-05 17:07 UTC
[R] I: a coloured band within each panel of a lattice bwplot
On Friday 05 November 2004 05:05, 8rino-Luca Pantani wrote:> Hi all, > I would like to add to each panel of a bwplot a coloured central > band, centered on the mean of the values, being its width +- 2% of > the mean itself. > > I know how to add lines, i.e. something like > > bwplot(X ~ Y|FACTOR > data=my.df, > panel= function(x, y){ > panel.bwplot(x, y) > panel.abline(v = mean(x, na.rm = T) - mean(x, na.rm = T) * > 0.02 panel.abline(v = mean(x, na.rm = T) + mean(x, na.rm = T) * 0.02 > }) > > but I cannot figure out how to shade the region included between the > two lines.Generally speaking, you want to draw a polygon . There is no lattice wrapper for the 'grid.polygon' function, so you will have to use it directly. Check out> library(grid) > help(grid.polygon)For this simpler case though, 'grid.rect' should be sufficient, e.g. panel= function(x, y){ m <- mean(x, na.rm = T) grid.rect(x = unit(m, "native"), w = unit(m * 0.04, "native"), gp = gpar(fill = "lightgrey", col = "transparent")) panel.bwplot(x, y) } Make sure to do a 'library(grid)' before this. Deepayan
Possibly Parallel Threads
- (Lattice) How to improve the readability of a bwplot, i.e. separating groups somehow
- indexing within the function "aggregate"
- I: differences between R and S (reproducing a plot from a book )
- again on ubuntu 7.10 and amd64
- Rearranging long tables, Sweave, xtable, LaTeX