Adding text to panels in lattice barchart

前端 未结 1 1369
别跟我提以往
别跟我提以往 2021-01-03 10: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:



        
相关标签:
1条回答
  • 2021-01-03 10:36

    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)
             }
    )
    

    enter image description here

    0 讨论(0)
提交回复
热议问题