更改与R/点阵中的多个面板关联的条带的背景和文本 [英] change background and text of strips associated to multiple panels in R / lattice

查看:114
本文介绍了更改与R/点阵中的多个面板关联的条带的背景和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我处理的示例.

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")

提供了第一个草图: 我怎样才能做到这一点?

解决方案

这是一个干净且易于自定义的解决方案.

myStripStyle(),传递给xyplot()strip=自变量的函数使用计数器变量which.panel选择颜色以及当前正在绘制的面板的factor.levels值. /p>

如果您想使用这些设置,只需在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)

The following is the example I work on.

require(lattice)
data(barley)
xyplot(yield ~ year | site, data = barley)

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆