Removing per-panel unused factors in a bar chart with nested factors

后端 未结 1 1006
死守一世寂寞
死守一世寂寞 2021-01-18 07:16

Some time ago I asked a question on how to remove unused factors in a bar chart, and I got a useful solution for that problem, thanks to @Aaron. Now, I am facing a very simi

相关标签:
1条回答
  • 2021-01-18 07:55

    To do this, you'll need to supply both a modified prepanel function (which sets up the limits of each panel's plotting area) and a modified panel function (which is responsible for plotting the data). In both cases, the key modification is to use x[, drop=TRUE] to drop the unused levels:

    library(lattice)
    
    barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, df, groups = METRIC,
             scales = list(x = list(rot = 45, relation = 'free')),
             prepanel = function(x,y,...) {
                 xx <- x[, drop = TRUE]
                 list(xlim = levels(xx),
                      xat=sort(unique(as.numeric(xx))))
             },
             panel = function(x,y,...) {
                 xx <- x[, drop = TRUE]
                 panel.barchart(xx, y, ...)
             },
             auto.key = list(columns = 2))
    

    enter image description here

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