Re: [R] annotating an axis in bwplot (lattice)

From: Deepayan Sarkar <deepayan.sarkar_at_gmail.com>
Date: Fri 23 Sep 2005 - 06:19:31 EST

On 9/20/05, Sebastian Luque <spluque@gmail.com> wrote:
> Hi,
>
> I'd like to add, say, the sample size for every group in a bwplot as a
> parenthetical annotation to the axis. Here's a sketch:
>
> --8<---------------cut here---------------start------------->8---
> require(Hmisc)
> age <- sample(1:100, 1000, replace = TRUE)
> sex <- gl(2, 8, 1000, c("Male", "Female"))
> grp <- gl(4, 6, 1000, letters[1:4])
>
> bwplot(grp ~ age | sex, aspect = 0.5, box.ratio = 2,
> panel = function(x, y, ...) {
> panel.bpplot(x, y, nout = 0.01, probs = seq(0.05, 0.45, 0.05))
> nage <- tapply(age, grp, length)
> panel.text(rep(0, length(x)), seq(along = x), labels = nage)
> })
> --8<---------------cut here---------------end--------------->8---
>
> I have two problems here: 1. place the sample size as a note in
> parenthesis next to axis annotation label for the group (e.g. a (252), b
> (252), c (250), d (246)), and 2. handle more complex subsetting in the
> call to bwplot, i.e. when using the 'data' and 'subset' arguments, so that
> 'nage' in the code above is more flexible.

It's not clear to me what you want to do. Do you want the sample size for the data in that panel, or for all the data? Your current implementation does the latter, and in a way that wouldn't work if you actually had a 'data' argument. If the former, then you should have done

bwplot(grp ~ age | sex, aspect = 0.5, box.ratio = 2,

      panel = function(x, y, ...) {
        panel.bwplot(x, y, nout = 0.01, probs = seq(0.05, 0.45, 0.05))
        nage <- tapply(x, y, length)
        panel.text(rep(0, length(x)), seq(along = x), labels = nage)
      })

in which case data and subset wouldn't be a problem. If you really want the frequencies for the whole (subsetted) data, you might as well use something like:

dd <- data.frame(age, sex, grp)

with(subset(dd, age > 20),

     bwplot(grp ~ age | sex, aspect = 0.5, box.ratio = 2,
	    ylim = {
	      tg <- table(grp)
	      paste(names(tg), "(", tg, ")")
            }))

Deepayan



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Sep 23 06:25:38 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:25 EST