自定义格子函数:使面板的颜色正确
Custom Lattice Function: Getting Colors Correct Across Panels
我正在尝试控制自定义格子函数中数据点的颜色。这个想法是响应可能是一个或两个因素的函数。数据点的颜色应由 fac1
确定。这是函数:
compareCats <-
function(formula = NULL, data = NULL, cols = NULL, ...) {
TwoFac <- FALSE
res <- as.character(formula[[2]])
if (length(formula[[3]]) == 1) {
fac1 <- as.character(formula[[3]])
}
if (length(formula[[3]]) == 3) {
fac2 <- as.character(formula[[3]][3])
fac1 <- as.character(formula[[3]][2])
TwoFac <- TRUE
}
args <- as.list(match.call(expand.dots = FALSE)[-1]) # used a bit later
if (TwoFac) keep <- c(res, fac1, fac2) # Reduce the df and clean it of NAs
if (!TwoFac) keep <- c(res, fac1)
data <- data[, keep]
data <- na.omit(data)
# cols2 is used for the data points according to levels in fac1
if (!TwoFac) cols2 <- cols[data[,fac1]] # works fine
if (TwoFac) {
# In this case, the points and panels are drawn in an order I don't understand
cols2 <- rep(NA_character_, nrow(data))
for (i in 1:nlevels(data[,fac1])) { # make the colors correspond to the original order
tmp <- which(data[,fac1] == levels(data[,fac1])[i])
cols2[tmp] <- cols[i]
}
}
data$cols <- cols2
p <- lattice::xyplot(as.formula(args$formula), # now the plot
data = eval(args$data), ...,
panel = function(x, y, ...) {
lattice::panel.xyplot(x, y, col = data$cols, ...)
}
)
return(p)
}
这是数据和两个函数调用:
set.seed(13)
mydf <- data.frame(
resp = rnorm(40),
cat1 = sample(LETTERS[1:3], 40, replace = TRUE),
cat2 = sample(letters[1:2], 40, replace = TRUE))
library("lattice")
library("plyr")
# One factor / works fine
p <- compareCats(formula = resp~cat1, data = mydf,
cols = c("red", "orange", "blue"))
print(p)
# Two factors / colors not assigned correctly
p <- compareCats(formula = resp~cat1 | cat2, data = mydf,
cols = c("red", "orange", "blue"))
print(p)
第一个生成此图:
第二个这个:
如何让第二个示例中的颜色从左到右依次为红色、橙色、蓝色、红色、橙色、蓝色?我尝试了很多方法,有时可以让第一个面板表现出来,但第二个面板似乎是随机的。显然我不太明白 Lattice 在 2 因子的情况下使用什么顺序,文档建议使用交互但仍然留下几种可能性,none 我已经能够弄清楚。
如果您想在面板上一致地更改点的颜色,我建议您通过更标准的 groups=
参数来实现。我会在您的 compareCats 函数中更改这两行
cols2 <- factor(cols2, levels=cols) # no need to attach to data
p <- lattice::xyplot(as.formula(args$formula), groups=cols2,
data = eval(args$data), ...,
par.settings=list(superpose.symbol=list(col=cols))
)
这里我们用groups=
给不同的组分配一个点,我们用superpose.symbol指定给每个组分配什么颜色。
我正在尝试控制自定义格子函数中数据点的颜色。这个想法是响应可能是一个或两个因素的函数。数据点的颜色应由 fac1
确定。这是函数:
compareCats <-
function(formula = NULL, data = NULL, cols = NULL, ...) {
TwoFac <- FALSE
res <- as.character(formula[[2]])
if (length(formula[[3]]) == 1) {
fac1 <- as.character(formula[[3]])
}
if (length(formula[[3]]) == 3) {
fac2 <- as.character(formula[[3]][3])
fac1 <- as.character(formula[[3]][2])
TwoFac <- TRUE
}
args <- as.list(match.call(expand.dots = FALSE)[-1]) # used a bit later
if (TwoFac) keep <- c(res, fac1, fac2) # Reduce the df and clean it of NAs
if (!TwoFac) keep <- c(res, fac1)
data <- data[, keep]
data <- na.omit(data)
# cols2 is used for the data points according to levels in fac1
if (!TwoFac) cols2 <- cols[data[,fac1]] # works fine
if (TwoFac) {
# In this case, the points and panels are drawn in an order I don't understand
cols2 <- rep(NA_character_, nrow(data))
for (i in 1:nlevels(data[,fac1])) { # make the colors correspond to the original order
tmp <- which(data[,fac1] == levels(data[,fac1])[i])
cols2[tmp] <- cols[i]
}
}
data$cols <- cols2
p <- lattice::xyplot(as.formula(args$formula), # now the plot
data = eval(args$data), ...,
panel = function(x, y, ...) {
lattice::panel.xyplot(x, y, col = data$cols, ...)
}
)
return(p)
}
这是数据和两个函数调用:
set.seed(13)
mydf <- data.frame(
resp = rnorm(40),
cat1 = sample(LETTERS[1:3], 40, replace = TRUE),
cat2 = sample(letters[1:2], 40, replace = TRUE))
library("lattice")
library("plyr")
# One factor / works fine
p <- compareCats(formula = resp~cat1, data = mydf,
cols = c("red", "orange", "blue"))
print(p)
# Two factors / colors not assigned correctly
p <- compareCats(formula = resp~cat1 | cat2, data = mydf,
cols = c("red", "orange", "blue"))
print(p)
第一个生成此图:
如果您想在面板上一致地更改点的颜色,我建议您通过更标准的 groups=
参数来实现。我会在您的 compareCats 函数中更改这两行
cols2 <- factor(cols2, levels=cols) # no need to attach to data
p <- lattice::xyplot(as.formula(args$formula), groups=cols2,
data = eval(args$data), ...,
par.settings=list(superpose.symbol=list(col=cols))
)
这里我们用groups=
给不同的组分配一个点,我们用superpose.symbol指定给每个组分配什么颜色。