Removing unused factors in a panel on a bar chart with nested factors

Some time ago I asked a question about how to remove unused factors in a histogram , and I got a useful solution for this problem, thanks to @Aaron. Now I have a very similar problem, but the solution that I used in the past does not work for this case.

This is the code to reproduce the data frame that I am using:

set.seed(17) df <- data.frame(BENCH = sprintf('bench-%s', sort(rep(letters[1:8], 4))), CLASS.CFG = sprintf('class-%s', c(rep('C', 4), rep('A', 4), rep('B', 8), rep('C', 8), rep('A', 4), rep('D', 4))), EXEC.CFG = rep(c('st', 'st', 'dyn', 'dyn'), 8), METRIC = rep(c('ipc', 'pwr'), 16), VALUE = runif(32)) 

Direct command to build this data frame:

 library(lattice) barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, df, groups = METRIC, scales = list(x = list(rot = 45, relation = 'free')), auto.key = list(columns = 2)) 

As you can see, I draw VALUE for each BENCH, limited by any possible combination of EXEC.CFG and CLASS.CFG (this is what I mean with nested factors), and using METRIC to create groups.

As a result, I get:

plot_with_gaps

Even if I use β€œfree” scales for the X axis, there are some (unnecessary) gaps between some bands on the graph (for example, between bench-b and bench-g ).

I tried to apply the solution I got for my previous question as follows:

 pl1 <- barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, subset(df, CLASS.CFG == 'class-A'), groups = METRIC, auto.key = list(columns = 2)) pl2 <- barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, subset(df, CLASS.CFG == 'class-B'), groups = METRIC) pl3 <- barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, subset(df, CLASS.CFG == 'class-C'), groups = METRIC) pl4 <- barchart(VALUE ~ BENCH | EXEC.CFG + CLASS.CFG, subset(df, CLASS.CFG == 'class-D'), groups = METRIC) pls <- c(pl1, pl2, pl3, pl4) pls <- update(pls, scales = list(y = 'same')) pls 

And this is the result that I get:

wrong_plot

As you noticed, this graph is incorrect. It does not take into account nested factors, mixing the values ​​from CLASS.CFG and EXEC.CFG in the panel names.

I struggled with this for a while, but to no avail. Does anyone know how this can be achieved? (Basically, what I'm trying to get is a plot similar to the first, but without spaces between bars).

+7
source share
1 answer

To do this, you need to provide both a modified prepanel function (which sets the limits of each area of ​​the panel construction) and a modified panel function (which is responsible for data construction). In both cases, the key modification is to use x[, drop=TRUE] to remove 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

+6
source

All Articles