以下是我正在处理的示例。
require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)

我想把不同的条带颜色不同的小树枝和字体颜色也是不同的背景颜色优化。例如:
strip background colors = c("black", "green4", "blue", "red", "purple", "yellow")
font color = c("white", "yellow", "white", "white", "green", "red")提供了第一个的粗略草图:

我如何才能做到这一点?
发布于 2011-12-17 01:15:59
这是一个干净且易于定制的解决方案。
myStripStyle()是传递给xyplot()的strip=参数的函数,它使用计数器变量which.panel为当前正在绘制的面板选择颜色和factor.levels的值。
如果您想尝试这些设置,只需在myStripStyle()的定义中的某个位置放置一个browser(),就可以了!
bgColors <- c("black", "green4", "blue", "red", "purple", "yellow")
txtColors <- c("white", "yellow", "white", "white", "green", "red")
# Create a function to be passed to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
panel.rect(0, 0, 1, 1,
col = bgColors[which.panel],
border = 1)
panel.text(x = 0.5, y = 0.5,
font=2,
lab = factor.levels[which.panel],
col = txtColors[which.panel])
}
xyplot(yield ~ year | site, data = barley, strip=myStripStyle)

发布于 2012-02-22 19:42:55
引用函数作用域之外的变量可能并不明智。
您可以使用par.strip.text将额外的参数传递给带状函数。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)https://stackoverflow.com/questions/8536239
复制相似问题