Hi:

The problem with your panel.text() code is that y = Count is either 0 or 1,
so the labels never get off the ground, so to speak. Since groups =
StimCount, you don't need to specify StimCount in panel.text(); the groups
argument does that for you. The essential issue, AFAICT, is to coordinate
panel.text's labels with the *stacked* bar chart - that's trickier than it
might appear, because (i) it has to determine which Counts are 1 in a
particular Rating/RateType/Temperature combination; (ii) it needs to know
how many such counts occur at each Rating/RateType/Temperature combination;
(iii) it needs to map the StimCounts associated with Count == 1 within
combination in numerical order; (iv) it needs to print the StimCounts in
ascending order. Basically, you want something like StimCounts[cumsum(Count
== 1)] | Rating * Type * Temperature. I'm trying to reproduce your plot in
ggplot2 and it's easy to get the bar charts, but it's not so obvious how to
align the text labels.

After about 20 minutes of playing around, I came up with the following.
Create the following variables:
    *  Lab = a variable that returns the numeric value of StimCount if Count
= 1, 0 otherwise
    *  ypos = variable that cumulates the positive counts if Count = 1, 0
otherwise
    *  col = logical: TRUE if ypos > 0, FALSE otherwise
Since I'm a bit better at ggplot2 than lattice, I decided to try this out in
ggplot2, and the following works for me. The primary difference is the
appearance of the legend and its placement. I also reduce the size of the
rating labels a tad to avoid overlapping.

library(ggplot2)
tf2 <- transform(tf, Lab = as.numeric(StimCount) * Count)

# ddply() is a function from the plyr package, which loads with ggplot2
tf2 <- ddply(tf2, .(Rating, RateType, Temperature), transform, ypos =
cumsum(Count) * Count)
tf2$col <- factor(tf2$ypos > 0)
g <- ggplot(tf2, aes(x = Rating, y = Count, fill = StimCount))
g + geom_bar(stack = TRUE) + facet_grid(RateType ~ Temperature) + theme_bw()
+
    scale_fill_manual(breaks = levels(tf$StimCount), values = rainbow(12)) +
    opts(axis.text.x = theme_text(size = 8)) +
    geom_text(aes(y = ypos - 0.5, colour = col, label = Lab), size = 3) +
    scale_colour_manual(values = c('FALSE' = 'transparent', 'TRUE' =
'black'), legend = FALSE) +
    opts(legend.position = c(0.88, 0.74))

Perhaps you can use the new variables I created to help you generate the
correct labeling in lattice. I've managed to get labels, but all of them are
repeated in each panel so the alignment is not correct. My last shot was

print(barchart(Count~Rating | RateType*Temperature, data=tf2,
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, ...) {
                      u <- ifelse( tf$Count == 1, as.numeric(tf$StimCount),
0)
                  panel.barchart(x, y, subscripts=subscripts, groups=groups,
...)
                  panel.text(x, tf2$ypos - 0.5, subscripts = subscripts,
groups = groups,
                             label=tf2$Lab, cex=1)
              }
      ))

I've managed to get the right labels stacked together, but they are not
placed where they should be, and unfortunately all of the stacks repeat in
all four panels. Hopefully someone else can fill in the missing piece.

HTH,
Dennis



On Tue, Mar 22, 2011 at 1:30 PM, David Perlman <dperlman@wisc.edu> wrote:

> 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 i!
>  s 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
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

