我有一个有两个条件的点阵图:一个条件(x1)有4个级别,一个有2个级别(x2)。该图有每个变量x1和x2的条带:
df <- data.frame(y = runif(100,0,10)
, x1 = rep(c("A","B","C","D"),25)
, x2 = as.numeric(c(runif(100)<0.5))
)
histogram( ~y | x1 + as.factor(x2), data=df)
我想改变x1的等级。
我尝试过:
histogram( ~y | x1 + as.factor(x2)
, data=df
, strip = strip.custom(
, factor.levels = c("no","yes"))
)
)
但我不知道如何分配级别条件x2。我很欣赏任何提示。
答案 0 :(得分:2)
搜索定义自己的条带函数的web找到的解决方案。
my.strip <- function(which.given, ..., factor.levels) {
levs <- if (which.given == 2) c("no","yes") #levels for your second factor (x2)
else c("AAA", "BBB", "CCC", "DDD") #levels for your first factor (x1)
strip.default(which.given, ..., factor.levels = levs)
}
histogram( ~y | x1 + as.factor(x2) , data=df, strip = my.strip)
library(latticeExtra)
useOuterStrips(histogram( ~y | x1 + as.factor(x2) , data=df),
strip =strip.custom(factor.levels = c("AAA","BBB","CCC","DDD")),
strip.left=strip.custom(factor.levels = c("no","yes")))