Adding text to panels in lattice barchart

2019-02-11 01:12发布

问题:

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!!!

回答1:

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:

  • Calculate the cumulative sum of x values for each y value
  • This is a classic split, apply, combine problem. You can use 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)
         }
)