删除带有嵌套因子的条形图中的每面板未使用因子 [英] Removing per-panel unused factors in a bar chart with nested factors
问题描述
前段时间,我在>如何删除中提出了一个问题条形图中未使用的因子,由于有了@Aaron,我为该问题找到了一个有用的解决方案.现在,我面临着一个非常相似的问题,但是我过去使用的解决方案不适用于这种情况.
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 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))
绘制此数据框的简单命令是:
The straightforward command to plot this data frame would be:
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))
如您所见,我正在为约束到EXEC.CFG和CLASS.CFG(这就是我所说的嵌套因子)的每种可能组合的每个BENCH绘制值,并使用METRIC创建组.
As you can see, I am plotting VALUE for each BENCH constrained to every possible combination of EXEC.CFG and CLASS.CFG (that is what I mean with nested factors), and using METRIC to create groups.
这是我获得的结果:
即使我在X轴上使用了自由"标度,在图中某些条之间(例如,在bench-b
和bench-g
之间)也存在一些(不必要的)间隙.
Even if I use 'free' scales for the X axis, in the plot there are some (unnecessary) gaps between some of the bars (e.g., between bench-b
and bench-g
).
我尝试通过以下方式应用针对上一个问题的解决方案:
I tried to apply the solution that I got for my previous question in the following way:
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
这是我得到的结果:
您会注意到,此图不正确.它不考虑嵌套因子,在面板标题中混合了CLASS.CFG和EXEC.CFG的值.
As you will notice, this plot is not correct. It does not respect the nested factors, mixing values from CLASS.CFG and EXEC.CFG in the panel titles.
我已经为此战斗了一段时间,但没有成功.有谁知道这是如何实现的? (基本上,我想要获得的是与第一个图类似的图,但是在条形图之间没有间隙.)
I have been fighting with this for a while, but with no success. Does anyone know how this can be achieved? (Basically, what I am trying to obtain is a plot like the first one, but without the gaps between the bars).
推荐答案
为此,您需要同时提供修改后的prepanel
函数(用于设置每个面板绘图区域的界限)和修改后的panel
函数(负责绘制数据).在这两种情况下,关键的修改是使用x[, drop=TRUE]
删除未使用的级别:
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))
这篇关于删除带有嵌套因子的条形图中的每面板未使用因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!