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:
library(lattice)
data(iris)
barchart(seq(1,50) ~ Petal.Width + Petal.Length | Species, data = iris, stack = TRUE,
panel=function(x, y, ...) {
panel.barchart(x, y, ...);
ltext(x=iris$Petal.Width/2, y=y, labels=iris$Petal.Width, cex = 0.5);
ltext(x=iris$Petal.Width + iris$Petal.Length/2, y=y, labels=iris$Petal.Width, cex = 0.5);
}
)
How would I do this right?
Bonus question:
Beside it does not work as expected, I think my code is not too efficient (especially seq(1,50)
and Petal.Width + Petal.Length
). Is there a better way?
Thank you in advance!!!
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
anddo.call
:xx <- do.call(c, unname(lapply(split(x, y), function(t)cumsum(t)-t/2)))
The code: