Richard.Cotton at hsl.gov.uk
2007-Dec-19 09:23 UTC
[R] Different labels by panel in barchart
Dear all,
I'm analysing a survey, and creating a barchart of the different responses
for each question. The questions are grouped according to a number of
categories, so I'm using lattice to create a plot with each question in a
category on it. The problem is that the response set for different
questions within the same category varies. I want to be able to draw only
the relevant bars, without spaces for nonrelevant reposnes. An example
may make it clearer:
testdf = data.frame(y=c(23,34,45, 56), x=factor(c("a", "a",
"b", "c")),
g=factor(c(1,2,1,2)))
barchart(y~x|g, data=testdf)
#In the first panel I would like two columns "a" and "b",
positioned at 1
and 2 respectively; in the second I would again like two columns at 1 and
2, this time labelled "a" and "c".
barchart(y~x|g, data=testdf, scales=list(x=list(relation="free")),
drop.unused.levels=TRUE)
#This is closer, but puts the columns in the wrong place in the second
panel.
Regards,
Richie.
Mathematical Sciences Unit
HSL
------------------------------------------------------------------------
ATTENTION:
This message contains privileged and confidential inform...{{dropped:20}}
On 12/19/07, Richard.Cotton at hsl.gov.uk <Richard.Cotton at hsl.gov.uk> wrote:> Dear all, > > I'm analysing a survey, and creating a barchart of the different responses > for each question. The questions are grouped according to a number of > categories, so I'm using lattice to create a plot with each question in a > category on it. The problem is that the response set for different > questions within the same category varies. I want to be able to draw only > the relevant bars, without spaces for nonrelevant reposnes. An example > may make it clearer: > > testdf = data.frame(y=c(23,34,45, 56), x=factor(c("a", "a", "b", "c")), > g=factor(c(1,2,1,2))) > barchart(y~x|g, data=testdf) > > #In the first panel I would like two columns "a" and "b", positioned at 1 > and 2 respectively; in the second I would again like two columns at 1 and > 2, this time labelled "a" and "c". > > barchart(y~x|g, data=testdf, scales=list(x=list(relation="free")), > drop.unused.levels=TRUE) > > #This is closer, but puts the columns in the wrong place in the second > panel.You need to do the dropping of the levels separately for each panel: barchart(y~x|g, data = testdf, scales=list(x=list(relation="free")), prepanel = function(x, y, ...) { x <- x[, drop = TRUE] prepanel.default.bwplot(x, y, ...) }, panel = function(x, y, ...) { x <- x[, drop = TRUE] panel.barchart(x, y, ...) }) -Deepayan