【问题标题】:Removing per-panel unused factors in a bar chart在条形图中删除每个面板未使用的因素
【发布时间】:2012-09-12 15:17:29
【问题描述】:

我正在尝试使用 lattice 中的条形图制作绘图,但对于给定面板,我遇到了一些未使用因子的问题。我曾尝试使用drop.unused.levels,但它似乎只会在未在任何面板中使用时丢弃因子。

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

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

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

这给了我以下情节:

如您所见,PI、PF 和 CS 类的基准之间存在差距。原因是每个因素只存在于给定的类中,因此在所有其他因素中都没有,barchart 可能会在 x 轴上引入间隙。

我的第二次尝试是四次致电barchart(每个班级一次):

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

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

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

【问题讨论】:

    标签: r plot lattice


    【解决方案1】:

    这是使用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
    

    【讨论】:

    • 这正是我所需要的!谢谢! :)
    【解决方案2】:

    我只是在具有 95 个级别和 lattice::xyplot 的因子中遇到了同样的问题。 对我有用的是(因子是具有太多级别的变量):

        library(gdata)
        key<-simpleKey(levels(drop.levels(df$factor)),...)
        xyplot(response~predictor,groups=factor, data=df, key=key)
    

    对我来说就像一个魅力。 祝你好运!

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 2015-01-16
      相关资源
      最近更新 更多