删除条形图中每个面板的未使用因素 [英] Removing per-panel unused factors in a bar chart

查看:68
本文介绍了删除条形图中每个面板的未使用因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用晶格中的条形图进行绘制,但是对于给定的面板,我有一些未使用因素的问题.我已经尝试过使用drop.unused.levels,但似乎只有在没有任何面板中使用这些因素时,它才会丢弃这些因素.

I am trying to make a plot using barchart from lattice, but I am having some issues with unused factors for a given panel. I have tried using drop.unused.levels but it seems it only drops factors when they are not used in any panel.

这是我正在使用的数据框:

This is the data frame that I am using:

dm <- structure(list(Benchmark = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), class = "factor", .Label = c("416.gamess", 
"429.mcf", "436.cactusADM", "458.sjeng", "462.libquantum", "471.omnetpp", 
"482.sphinx3")), Class = structure(c(3L, 1L, 2L, 3L, 1L, 4L, 
2L, 3L, 1L, 2L, 3L, 1L, 4L, 2L, 3L, 1L, 2L, 3L, 1L, 4L, 2L, 3L, 
1L, 2L, 3L, 1L, 4L, 2L, 3L, 1L, 2L, 3L, 1L, 4L, 2L), class = "factor", .Label = c("CS", 
"PF", "PI", "PU")), Config = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Disabled", 
"Shallowest", "Deepest", "StorePref", "StridedPref"), class = "factor"), 
    Perf = c(1, 0.72, 0.8, 1, 0.32, 1.16, 0.79, 1, 0.98, 1, 1, 
    0.72, 1, 0.99, 1, 0.98, 1, 1, 1.12, 0.97, 1, 1, 0.97, 1, 
    1, 0.99, 0.97, 1, 1, 1.18, 1, 1, 0.99, 0.97, 1)), .Names = c("Benchmark", 
"Class", "Config", "Perf"), row.names = c(NA, -35L), class = "data.frame")

首先,我尝试使用barchart这样:

First I attempted using barchart like this:

barchart(Perf ~ Benchmark | Class, dm, groups=Config,
         scales=list(x=list(relation='free')), auto.key=list(columns=3))

那给了我以下情节:

如您所见,PI,PF和CS类的基准之间存在差距.原因是每个因素仅存在于给定的类别中,因此所有其他因素均不存在,并且barchart可能会在x轴上引入间隙.

As you can see, there is a gap between the benchmarks for PI, PF and CS classes. The reason is that each factor is only present in a given class, thus it is missing in all the others, and barchart might introduce a gap in the x axis.

我的第二次尝试是呼叫四次barchart(每个班一次):

My second attempt was to call barchart four times (one for each class):

class.subset <- function(dframe, class.name) {
    return(dframe[dframe$Class == class.name, ])
}

pl1 <- barchart(Perf ~ Benchmark, class.subset(dm, 'PI'), groups=Config)
pl2 <- barchart(Perf ~ Benchmark, class.subset(dm, 'PF'),, groups=Config)
pl3 <- barchart(Perf ~ Benchmark, class.subset(dm, 'CS'),, groups=Config)
pl4 <- barchart(Perf ~ Benchmark, class.subset(dm, 'PU'),, groups=Config)

print(pl1, split=c(1, 1, 2, 2), more = TRUE)
print(pl2, split=c(1, 2, 2, 2), more = TRUE)
print(pl3, split=c(2, 1, 2, 2), more = TRUE)
print(pl4, split=c(2, 2, 2, 2))

我得到的图几乎是我想要的,但是现在我不知道如何为所有子图创建单个全局图例(而不是为每个子图创建相同的图例):

The plot that I got is pretty much what I want, but now I do not know how to create a single global legend for all the subplots (instead of the very same legend for each subplot):

理想情况下,我更愿意使用第一种方法解决我面临的问题(因为那样的话,我在每个面板中也都有类名).但是,如果在第二种情况下,可以为每个包含类名称的子图添加全局图例和标题,也可以.

Ideally, I would prefer to solve the problem that I am facing using the first approach (since in that way I would also have the class name in each of the panels). However, if in the second case, it is possible to add a global legend and a title for each subplot containing the class name, that would be okay too.

推荐答案

以下是使用latticeExtra的快速方法:

Here's a quick way using latticeExtra:

pl1 <- barchart(Perf ~ Benchmark|Class, class.subset(dm, 'PI'), groups=Config, 
                auto.key=list(columns=3))
pl2 <- barchart(Perf ~ Benchmark|Class, class.subset(dm, 'PF'), groups=Config)
pl3 <- barchart(Perf ~ Benchmark|Class, class.subset(dm, 'CS'), groups=Config)
pl4 <- barchart(Perf ~ Benchmark|Class, class.subset(dm, 'PU'), groups=Config)

library(latticeExtra)
pls <- c(pl1, pl2, pl3, pl4)
pls <- update(pls, scales=list(y="same"))
pls

这篇关于删除条形图中每个面板的未使用因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆