【问题标题】:How to set x-labels in a box made in lattice::bwplot that represent fractions without altering position of boxes and avoiding long decimals如何在 lattice::bwplot 制作的盒子中设置 x-labels 来表示分数而不改变盒子的位置并避免长小数
【发布时间】:2016-11-28 23:13:15
【问题描述】:

我是 lattice 的新手,我需要创建一个箱线图,其中 y 值代表电流。数据按从 1/24 到 8/1 的不同比率进行整理。幸运的是,我得到了框的相对位置以尊重实际比率。我想要实现的是使用 x 标签,例如“1/24”、1/12 等,而不是“0.04166667”、0.08333333 等。您可以看到下面的代码示例。我在面板函数内部尝试了不同的方法,还探索了 scales 参数。数据经过调节并产生 4 个面板。棘手的一点是右侧的面板和左侧的面板具有不同的 x 范围。

代码:

bwplot(current~factor(ac.ratio)|factor(constant.electrode)+factor(substrate), data=current.df, 
       scales=list(x=list(relation="free",
                          at=c(1/24, 1/12, 1/6, 1/3, 2/3, 1, 2, 4, 8),
                          labels = FALSE), 
                   y="same"), 
       layout=c(2,2), index.cond=list(2:1, 2:1))

我得到的一个例子是:

我需要的是类似于:

左侧面板中的位置也必须重叠(就像右侧面板中的位置一样)。这就是我需要的(面板标题仅作为示例,不是最终的)。非常感谢。

【问题讨论】:

  • 查看MASS包中的fractions函数(library(MASS)
  • 谢谢。这样就可以了。

标签: r lattice bwplot


【解决方案1】:

这是一个使用xyplot 代替bwplot 并在内部调用panel.bwplot 的解决方案。我不得不手动调整box.width 以获得合理的结果。

library(lattice)

dd <- data.frame(
  current = rnorm(200, 350, 50),
  ac.ratio = sample(c(1/24, 1/12, 1/6, 1/3), 200, replace = TRUE),
  substrate = gl(2, k = 100, labels = c("Acetate", "OECD"))
)

xyplot(current ~ ac.ratio | substrate, data = dd, box.width = 0.02,
       scales = list(x = list(at = c(1/24, 1/12, 1/6, 1/3),
                              labels = c("1/24", "1/12", "1/6", "1/3"))),
       panel = panel.bwplot, horizontal = FALSE)

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    相关资源
    最近更新 更多