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. I have a feeling the 'subscripts' argument may be useful for the second issue, but I'm not discovering how. For the first point, I'll have to play some more with 'scales' argument and its 'at' and 'labels' components. Any suggestions? Thanks in advance, -- Sebastian P. Luque
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
Hi Deepayan, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote: [...]> 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, ")") > }))Thanks, this is almost what I was looking for. Because I needed the frequency of each group in *each* panel, I modified your suggestion like (omitting the panel function for briefness): --8<---------------cut here---------------start------------->8--- with(subset(dd, age > 20), bwplot(grp ~ age | sex, aspect = 0.5, box.ratio = 2, scales = list( y = list(relation = "free")), ylim = { tgf <- table(grp[sex == "Female"]) tgm <- table(grp[sex == "Male"]) list(paste(names(tgf), "(", tgf, ")"), paste(names(tgm), "(", tgm, ")")) })) --8<---------------cut here---------------end--------------->8--- One has to be careful though with the order of panels. Thank you, -- Sebastian P. Luque
On 9/22/05, Sebastian Luque <spluque at gmail.com> wrote:> Hi Deepayan, > > > Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote: > > [...] > > > 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, ")") > > })) > > Thanks, this is almost what I was looking for. Because I needed the > frequency of each group in *each* panel, I modified your suggestion like > (omitting the panel function for briefness):This might be a better approach then: bwplot(grp ~ age | sex, aspect = 0.5, box.ratio = 2, scales = list(y = "free", rot = 0), prepanel = function(x, y, ...) { tg <- table(y); list(ylim = paste(names(tg), "(", tg, ")")) } ) Deepayan