[R] annotating an axis in bwplot (lattice)
Deepayan Sarkar
deepayan.sarkar at gmail.com
Thu Sep 22 22:19:31 CEST 2005
On 9/20/05, Sebastian Luque <spluque at 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
More information about the R-help
mailing list