RINNER Heinrich
2009-Dec-02 07:57 UTC
[R] lattice: adding information on number of observations
Dear R-users, I am using R version 2.9.1 and lattice 0.17-26 under windows. In a lattice boxplot, I would like to add information on how many observations each singel boxplot is based upon. For example (the basic plot): # Begin R-code library(lattice) dat <- data.frame(panvar = rep(c("A","B","A","B"), c(3,7,4,6)), grp = rep(c("grp1", "grp2"), c(10,10)), val = rnorm(20)) dat bwplot(val ~ grp | panvar, data = dat) # End R-code As in my real life data the number of observations in each subgroup will be quite different, I would like to include the number of observations each boxplot is based upon. Aggregation will tell me the numbers: # Begin R-code aggregate(dat$val, by = list(dat$panvar, dat$grp), FUN = length) # End R-code But how to match these numbers with the plot (so that for example "n=3" will be written as text beneath panel A/group1)? Any advice would be appreciated; kind regards Heinrich.
Felix Andrews
2009-Dec-02 09:48 UTC
[R] lattice: adding information on number of observations
Depends on exactly how / where you want to display the text. One approach (showing text inside each panel, at the bottom): bwplot(val ~ grp | panvar, data = dat, panel = function(x, y, ...) { panel.bwplot(x, y, ...) nn <- table(x) panel.text(paste("n =", nn), x = seq_along(nn), y = current.panel.limits()$y[1], pos = 3) }) 2009/12/2 RINNER Heinrich <HEINRICH.RINNER at tirol.gv.at>:> Dear R-users, > > I am using R version 2.9.1 and lattice 0.17-26 under windows. > In a lattice boxplot, I would like to add information on how many observations each singel boxplot is based upon. > > For example (the basic plot): > # Begin R-code > library(lattice) > dat <- data.frame(panvar = rep(c("A","B","A","B"), c(3,7,4,6)), grp = rep(c("grp1", "grp2"), c(10,10)), val = rnorm(20)) > dat > bwplot(val ~ grp | panvar, data = dat) > # End R-code > > As in my real life data the number of observations in each subgroup will be quite different, I would like to include the number of observations each boxplot is based upon. > Aggregation will tell me the numbers: > # Begin R-code > aggregate(dat$val, by = list(dat$panvar, dat$grp), FUN = length) > # End R-code > But how to match these numbers with the plot (so that for example "n=3" will be written as text beneath panel A/group1)? > > Any advice would be appreciated; > kind regards > Heinrich. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Felix Andrews / ??? Postdoctoral Fellow Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andrews at anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix/
RICHARD M. HEIBERGER
2009-Dec-02 16:22 UTC
[R] lattice: adding information on number of observations
The boxplot with sample size information is one of the features included in the HH package. An example is posted on my site http://astro.ocis.temple.edu/~rmh/HH/bwplot-color.pdf In this example I show grouping, control of x-axis position, multiple x-axis labels, and control of display of extreme outliers. Rich
Reasonably Related Threads
- tapply and more than one function, with different arguments
- lattice: defining an own function using args for "formula" and "groups"
- RODBC and Oracle: error "table does not exist"
- read.spss (package foreign) and character columns
- within: order of newly added variables