更改与 R/格子中的多个面板相关联的条带的背景和文本 [英] change background and text of strips associated to multiple panels in R / lattice
问题描述
以下是我处理的示例.
require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)
我想为不同的 sprips 设置不同的条带颜色,字体颜色也随背景颜色不同而优化.例如:
I want to put different strip color for different sprips and font color is also different optimized with the backgroud color. For example:
strip background colors = c("black", "green4", "blue", "red", "purple", "yellow")
font color = c("white", "yellow", "white", "white", "green", "red")
提供了第一个的粗略草图:我怎样才能做到这一点?
Rough sketch of the first one is provided: How can I achieve this?
推荐答案
这是一个干净且易于定制的解决方案.
Here's a clean and easily customizable solution.
myStripStyle()
,传入xyplot()
的strip=
参数的函数使用了计数器变量which.panel
为当前正在绘制的面板选择颜色以及 factor.levels
的值.
myStripStyle()
, the function that is passed in to the strip=
argument of xyplot()
uses the counter variable which.panel
to select colors and also the value of factor.levels
for the panel that's currently being plotted.
如果你想玩弄这些设置,只需在 myStripStyle()
的定义中的某处放置一个 browser()
并使用它!
If you want to play around with the settings, just put a browser()
somewhere inside the definition of myStripStyle()
and have at it!
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)
这篇关于更改与 R/格子中的多个面板相关联的条带的背景和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!