[R] Group labels in lattice barchart

David Perlman dperlman at wisc.edu
Tue Mar 22 21:30:31 CET 2011


Hello, I've been searching on the web for a few hours and seem to be stuck on this.  The code pasted below generates a histogram of subject responses in four different conditions in an experiment.  This version of the graph is one I'm using for internal consistency checking, so I've set it up to indicate the order of the responses, which is contained in the variable StimCount.  The purpose of this is to be able to compare across the panels to see if a similar outlier in two panels happened at about the same time.  Right now the stacked group bars are colored to indicate this StimCount number, which is OK but I have been trying to figure out how to indicate that with a simple number on top of the relevant stacked bar segment.  I haven't found any useful information on the web, except one response to someone asking a similar question on Stack Overflow, which stated that it is "easy enough if you know R" but that they can't post the answer due to licensing restrictions, which is pretty much an anti-helpful answer.
http://stackoverflow.com/questions/2147084/r-add-labels-to-lattice-barchart

In any case, as you can see from running the code below, right now I have something that puts numbers on the barchart, but with several failures:
1. The numbers are never higher than one bar segment; they don't go up with the stacked segments.
2. The labels are wrong; the bar segments seem to be labeled with the order of the segment, not with the underlying value of the grouping variable StimCount (as is shown in the key at the bottom).  I know this is because I have "label=groups" instead of "label=StimCount", but I can't for the life of me figure out how to get access to the actual value of StimCount inside my custom panel function.  I have tried several different approaches, such as label=StimCount or label=data$StimCount (adjusting the argument list as seemed appropriate, too) but nothing I have been able to think of worked for that.

Just to clarify, what I want is for each bar segment to have a number that matches its color, as specified in the key at the bottom.


As an aside, in the help for panel.barchart (as in many other places) it says "... extra arguments will be accepted but ignored".  I understand what that means but I have struggled to figure out any way of determining what the actual contents of the "..." will be, so I know what inputs are available to me in my custom panel function.  This seems like a very critical piece of information which, for some reason, is kept well-hidden...

Any help will be greatly appreciated!  

The following commands should produce an example plot by pasting directly into R, assuming you have a net connection:

library(lattice)
load(url('http://brainimaging.waisman.wisc.edu/~perlman/testdata.rdata'))

print(barchart(Count~Rating | RateType*Temperature, data=tf, groups=StimCount,
stack=TRUE, scales=list(alternating=c(3)), ylim=c(0,11),
par.settings=list(superpose.polygon=list(col=rainbow(10))),
auto.key = list(points = FALSE, rectangles = TRUE, space = "bottom", columns=5),
panel=function(x, y, subscripts, groups, ...) {
  panel.barchart(x, y, subscripts=subscripts, groups=groups, ...) 
  panel.text(x, y-0.5, label=groups, cex=1)
}
))


Note: this does not work, I get an error message on the plot that says "argument "data" is missing":
myPanel <- function(x, y, data, ...) {
  panel.barchart(x, y,  ...) 
  panel.text(x, y-0.5, label=data$StimCount, cex=1)
}


--
-dave----------------------------------------------------------------
"Pseudo-colored pictures of a person's brain lighting up are 
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph.  That could be a big problem if the goal is to get to the
truth."  -Dr. Steven Hyman, Harvard



More information about the R-help mailing list