I try to add labels to bars in a lattice barchart with multiple panels. I end up with way too many labels (every label is in every panel).
Here is my code:
The underlying question here is how to add labels to a stacked barchart in lattice
. The answer is provided in this question, but since the linked answer doesn't have multiple panels, I recreate a simpler answer using base R here:
You have to modify the panel function as follows:
plyr
for this (as in the linked answer), or, as I illustrate, split
and do.call
:xx <- do.call(c, unname(lapply(split(x, y), function(t)cumsum(t)-t/2)))
The code:
barchart( 1:10 ~ Petal.Width + Petal.Length | Species,
data = iris[c(1:10, 51:60, 101:110), ],
stack = TRUE,
panel=function(x, y, ...) {
panel.barchart(x, y, ...)
xx <- do.call(c, unname(lapply(split(x, y), function(t)cumsum(t)-t/2)))
ltext(xx, y=y, labels=x)
}
)