引用函数范围之外的变量可能不明智。
您可以使用par.strip.text
将其他参数传递给 strip 函数。par.strip.text
可以在绘图级别定义,通常用于设置文本显示属性,但是作为列表,您可以使用它将变量带入 strip 函数。
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, par.strip.text,
custBgCol=par.strip.text$custBgCol,
custTxtCol=par.strip.text$custTxtCol,...) {
panel.rect(0, 0, 1, 1,
col = custBgCol[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = custTxtCol[which.panel])
}
xyplot(yield ~ year | site, data = barley,
par.strip.text=list(custBgCol=bgColors,
custTxtCol=txtColors),
strip=myStripStyle)