Re: [R] Different labels by panel in barchart

From: Deepayan Sarkar <>
Date: Wed, 19 Dec 2007 08:45:48 -0800

On 12/19/07, <> 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 mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Wed 19 Dec 2007 - 16:54:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 19 Dec 2007 - 17:30:20 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.