请考虑这个功能:
tf <- function(formula = NULL, data = NULL, groups = NULL) {
grv <- eval(substitute(groups), data, environment(formula)) # the values
grn <- as.character(match.call()$groups) # the name
gr <- match.call()$groups # unquoted name
p <- xyplot(formula, data, # draws the data but not in groups
# Try these options:
# p <- xyplot(formula, data, groups, # can't find 'cat2'
# p <- xyplot(formula, data, groups = data[,grn], # can't fine grn
# p <- xyplot(formula, data, groups = grv, # can't find grv
panel = function(x, y) {
panel.stripplot(x, y, jitter.data = TRUE, pch = 20)
}
)
p
}
您可以使用它运行:
tf(formula = mpg~vs, groups = am, data = mtcars)
groups
将论点传递给我做错了什么xyplot
-为什么找不到?我无法弄清楚它如何想要这些group
信息。谢谢。
更新:
@agstudy 的回答很有帮助,但是如果我像原始示例一样添加面板功能,仍然无法识别组(没有分组,但也没有发生错误):
tf <- function(formula = NULL, data = NULL, groups = NULL) {
ll <- as.list(match.call(expand.dots = FALSE)[-1])
p <- xyplot(as.formula(ll$formula),
data = eval(ll$data),
groups = eval(ll$groups),
panel = function(x, y) {
panel.stripplot(x, y, jitter.data = TRUE, pch = 20)
}
)
p
}
仍然缺少一些东西...谢谢。