0

当我执行以下代码时(数据集是 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"))),  ...)
       }
      ) 

我得到以下图表:

在此处输入图像描述

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

谢谢。

4

1 回答 1

0

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

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(...)
       }
) 

在此处输入图像描述

于 2017-04-23T10:43:06.513 回答