[R] Adding labels into lattice's barchart

Deepayan Sarkar deepayan.sarkar at gmail.com
Mon Feb 14 11:43:57 CET 2011


On Wed, Feb 9, 2011 at 11:04 PM, Luca Meyer <lucam1968 at gmail.com> wrote:
> *** APOLOGIZES FOR THOSE READING THE LIST THROUGH NABBLE THIS WAS ALREADY POSTED THERE BUT NOT FORWARDED TO THE LIST FOR SOME UNKNOWN REASON ***
>
> I have a dataset that looks like:
>
> $ V1: factor with 4 levels
> $ V2: factor with 4 levels
> $ V3: factor with 2 levels
> $ V4: num (summing up to 100 within V3 levels)
> $ V5: num (nr of cases for each unique combination of V1*V2*V3 levels)
>
> Quite new to lattice - I've started reading Deepayan's book a few days ago - I have written the following:
>
> barchart(V2 ~ V4 | V1,
>         data=d1,
>         groups=V3,
>         stack=TRUE,
>         auto.key= list(space="top"),
>         layout = c(1,4),
>         xlab=" "
>         )
>
> which works just fine as a stacked bar chart with bars adding up to 100%. Now what I would like to see is the number of cases showing next to the 4 x-axis's labels - i.e. V2_L1, ... V2_L4.
>
> In other words now I see something like:
>
> *** V1_L1 ***
> V2_L4 AAAVVVVVVV
> V2_L3 AAVVVVVVVV
> V2_L2 AAAAAVVVVV
> V2_L1 AAVVVVVVVV
> *** V1_L2 ***
> V2_L4 AAAAAAVVVV
> V2_L3 AVVVVVVVVV
> etc...
>
> But what I am looking for is something like:
> *** V1_L1 ***
> V2_L4 (n=60) AAAVVVVVVV
> V2_L3 (n=10) AAVVVVVVVV
> V2_L2 (n=52) AAAAAVVVVV
> V2_L1 (n=15) AAVVVVVVVV
> *** V1_L2 ***
> V2_L4 (n=18) AAAAAAVVVV
> V2_L3 (n=74) AVVVVVVVVV
> etc...
>
> How can I do that? I have tried:
>
> V6 <- paste(V2," (n",V5,")")

What you really want is to compute the total sum of V5 per level of V2
(and add that to the labels of V2). There are many ways of doing so,
one is tapply().

In the absence of a reproducible example, here is an approximation:

tdf <- as.data.frame.table(apply(Titanic, c(1, 2, 4), sum))
names(tdf)[1:3] <- paste("V", 1:3, sep = "")

str(tdf)

barchart(V2 ~ Freq | V1, data=tdf, groups=V3, stack=TRUE)

with(tdf, tapply(Freq, V2, sum))

numByV2 <- with(tdf, tapply(Freq, V2, sum))

barchart(V2 ~ Freq | V1, data = tdf, groups = V3, stack=TRUE,
         ylim = sprintf("%s (n=%g)", names(numByV2), numByV2))

## or

levels(tdf$V2) <- sprintf("%s (n=%g)", levels(tdf$V2), numByV2)
barchart(V2 ~ Freq | V1, data=tdf, groups=V3, stack=TRUE)

-Deepayan

>
> but what i get when I run
>
> barchart(V6 ~ V4 | V1,
>         data=d1,
>         groups=V3,
>         stack=TRUE,
>         auto.key= list(space="top"),
>         layout = c(1,4),
>         xlab=" "
>         )
>
> is a bunch of empty bars due to the fact that the unique combinations have risen.
>
> Any help would be appreciated.
>
> Thanks,
> Luca
>
> Mr. Luca Meyer
> www.lucameyer.com
> ______________________________________________
> 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.
>



More information about the R-help mailing list