*** 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,")")
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
Adding labels into lattice's barchart
5 messages · Luca Meyer, Deepayan Sarkar
4 days later
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.
Thanks Deepayan,
What you suggest is quite fine, but provides the overall number of cases for the entire dataset splitted into V2 levels.
What about if I need to show panel specific's values? For instance I want to show not the total number of Female but the total number of Female in 1st Class.
In other worlds, take your example and suppose I have:
barchart(V2 ~ Freq | V1, data = tdf, groups = V3, layout=c(1,4), stack=TRUE,
ylim = sprintf("%s (n=%g)", names(numByV2), numByV2))
and now what I would like to show is the result of
with(tdf, tapply(Freq, list(V2,V1), sum))
next to each stacked bar.
In the previous example, I would need show in the Crew panel Female (n=23), in the 3rd Class panel Female (n=196), etc...
Can I do that?
Thanks,
Luca
Il giorno 14/feb/2011, alle ore 11.43, Deepayan Sarkar ha scritto:
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.
Thanks Deepayan,
What you suggest is quite fine, but provides the overall number of cases for the entire dataset splitted into V2 levels.
What about if I need to show panel specific's values? For instance I want to show not the total number of Female but the total number of Female in 1st Class.
In other worlds, take your example and suppose I have:
barchart(V2 ~ Freq | V1, data = tdf, groups = V3, layout=c(1,4), stack=TRUE,
ylim = sprintf("%s (n=%g)", names(numByV2), numByV2))
and now what I would like to show is the result of
with(tdf, tapply(Freq, list(V2,V1), sum))
next to each stacked bar.
In the previous example, I would need show in the Crew panel Female (n=23), in the 3rd Class panel Female (n=196), etc...
Can I do that?
Thanks,
Luca
Il giorno 14/feb/2011, alle ore 11.43, Deepayan Sarkar ha scritto:
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.
On Mon, Feb 14, 2011 at 5:16 PM, Luca Meyer <lucam1968 at gmail.com> wrote:
Thanks Deepayan,
What you suggest is quite fine, but provides the overall number of cases for the entire dataset splitted into V2 levels.
What about if I need to show panel specific's values? For instance I want to show not the total number of Female but the total number of Female in 1st Class.
In other worlds, take your example and suppose I have:
barchart(V2 ~ Freq | V1, data = tdf, groups = V3, layout=c(1,4), stack=TRUE,
? ? ? ?ylim = sprintf("%s (n=%g)", names(numByV2), numByV2))
and now what I would like to show is the result of
with(tdf, tapply(Freq, list(V2,V1), sum))
next to each stacked bar.
In the previous example, I would need show in the Crew panel Female (n=23), in the 3rd Class panel Female (n=196), etc...
Can I do that?
Well, then you probably don't want to change the labels, but rather
put in the counts in each panel. Something like
barchart(V2 ~ Freq | V1, data=tdf, groups=V3, stack=TRUE,
panel = function(x, y, ...) {
n <- tapply(x, y, sum)
panel.barchart(x, y, ...)
panel.text(x = 0, y = sort(unique(as.numeric(y))),
srt = 90, adj = c(0.5, -0.2),
labels = sprintf("(n = %g)", n))
})
If the numbers you want to add up is not the x or y variable, then you
need a little bit more work. You will need to pass in the count
variable separately and use 'subscripts' to get the proper subsets
(look for discussions of 'subscripts' in the documentation and/or
book).
-Deepayan