如何使用包 "latticeExtra" 中的函数 "useOuterStrips" 手动给条带上色?

How to manually colour strips using function "useOuterStrips" in package "latticeExtra"?

我想做一个与下面的情节非常相似的情节。但是我想为每个条带使用不同的颜色,例如绿色的 sub1,蓝色的 sub2,红色的 sub3 ...(所有 seas1 到 seas4 都相同)。到目前为止,我没有成功做到这一点。

我在这里 post 看过这个:change background and text of strips associated to muliple panels in R / lattice 但我没能修改它来实现我想要的。我认为这是由于使用函数 useOuterStrips.

我使用 R 版本 3.2.0、latticeExtra_0.6-26 和 lattice_0.20-31:

用以下代码生成了图形
require(lattice)
require(latticeExtra)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
mydat <- data.frame(response=rnorm(400,mean=1),
                p = factor(sample(rep(1:4,each=100))),
                sub = factor(rep(sprintf("sub%i",1:4),each=100)),
                seas=factor(rep(sprintf("seas%i",1:4),100)))
useOuterStrips(bwplot(response~factor(p)|factor(sub)+factor(seas),
                  data=mydat,par.settings = list(strip.background=list(col = c("skyblue","gold"))),
                  fill = cbPalette,xlab="xlab",ylab="ylab"))

非常感谢任何帮助!

为了将来参考,这可以通过指定处理左侧条带的第二个函数来实现。请参阅 ?useOuterStrips 中的参数 strip.left

建立在上述 SO post

之上
## set strip background colors
cbPalette = c(
  "#999999", "#E69F00", "#56B4E9", "#009E73" # top
  , "#F0E442", "#0072B2", "#D55E00", "#CC79A7" # left
)

## define core strip function
myStripStyle = function(which.panel, factor.levels, col, ...) {
  panel.rect(
    0, 0, 1, 1
    , col = col[which.panel]
    , border = 1
  )
  panel.text(
    x = 0.5
    , y = 0.5
    , lab = factor.levels[which.panel]
    , ...
  )
}

## and convenience functions for top ..
myStripStyleTop = function(which.panel, factor.levels, ...) {
  myStripStyle(
    which.panel
    , factor.levels
    , col = cbPalette[1:4]
  )
}

## .. and left strips
myStripStyleLeft = function(which.panel, factor.levels, ...) {
  myStripStyle(
    which.panel
    , factor.levels
    , col = cbPalette[5:8]
    , srt = 90 # and other arguments passed to `panel.text()`
  )
}

## assemble plot
useOuterStrips(
  bwplot(
    response ~ factor(p) | factor(sub) + factor(seas)
    , data = mydat
    , fill = cbPalette
  )
  , strip = myStripStyleTop
  , strip.left = myStripStyleLeft
)