【问题标题】:How can I get alternating colors of the box rectangles in lattice bwplot?如何在格 bwplot 中获得矩形框的交替颜色?
【发布时间】:2017-04-21 19:23:39
【问题描述】:

当我执行以下代码时(数据集是 lattice 包的一部分):

ngroups <- length(unique(barley$site)) + 1

bwplot(yield ~ variety, data = barley, box.width = 1/ ngroups,
       groups = year, scales=(x=list(rot=45)), 
       auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
       par.settings=list(box.rectangle = list(col=c("red", "green"), lwd=3),
                         superpose.polygon=list(col=c("green", "red"), pch=c(15,15))
                         ),
       panel.groups = function(x, y, ..., group.number) {
         panel.bwplot(x + (group.number-1.5)/ngroups, y, ...)
       },
       panel=function(...) {
         panel.grid(h = -1, v=0)
         panel.superpose(par.settings=list(box.rectangle=list(col=c("green", "red"))),  ...)
       }
      ) 

我得到以下图表:

如何让盒子从左到右交替显示红色和绿色? (我注意到如果我删除自定义面板效果很好,但我想保留灰色参考线。)

谢谢。

【问题讨论】:

    标签: r lattice bwplot


    【解决方案1】:

    如果您愿意使用填充而不是边框​​来区分年份,我有一个解决方案。我还在一个看起来错误的地方切换了颜色的顺序(?),并删除了一些冗余代码。

    bwplot(yield ~ variety, data = barley, box.width = 1 / ngroups,
           groups = year, scales = (x = list(rot = 45)),
           par.settings = list(superpose.polygon = list(col = c("green", "red"),
                                                        pch =c (15, 15)),
                             superpose.symbol = list(fill = c("green", "red"))),
           auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
           panel.groups = function(x, y, ..., group.number) {
             panel.bwplot(x + (group.number - 1.5) / ngroups, y, ...)
           },
           panel = function(...) {
             panel.grid(h = -1, v = 0)
             panel.superpose(...)
           }
    ) 
    

    【讨论】:

    • 感谢您的回答,但我真的很想更改边框。当变化非常小时,只有伞形和边框变得可见,这就是我需要更改它们的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2018-08-22
    • 2022-10-05
    相关资源
    最近更新 更多