[R] lattice: adding information on number of observations

Felix Andrews felix at nfrac.org
Wed Dec 2 10:48:11 CET 2009


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/




More information about the R-help mailing list