我在试着做新的衣服和数据。我尝试了来自这个StatChull的格列奈特代码。我的目标是操纵一个外部参数,这不是一个审美价值。是这样的:
stat_custom(data = df, mapping = aes(x = xval, y = val), myparam = myval, geom = "custom")问题是,当我使用compute_group()创建自定义stat时,我可以获得自定义参数。一旦我将compute_group()更改为compute_layer(),程序就会停止工作。
下面是stat_chull()的工作程序:
StatChull <- ggproto("StatChull", Stat,
compute_group = function(self, data, scales, params, na.rm, myparam) {
message("My param has value ", myparam)
# browser()
data[chull(data$x, data$y), , drop = FALSE]
},
required_aes = c("x", "y")
)
stat_chull <- function(mapping = NULL, data = NULL, geom = "polygon",
position = "identity", na.rm = FALSE, myparam = "", show.legend = NA,
inherit.aes = TRUE, ...) {
layer(
stat = StatChull, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, myparam = myparam, ...)
)
}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_chull(fill = NA, colour = "black", myparam = "myval")这将打印在控制台上:
My param has value myval当我将compute_group()更改为compute_layer()时,此程序会出错
StatChull <- ggproto("StatChull", Stat,
compute_layer = function(self, data, scales, params, na.rm, myparam) {
message("My param has value ", myparam)
# browser()
data[chull(data$x, data$y), , drop = FALSE]
},
required_aes = c("x", "y")
)
stat_chull <- function(mapping = NULL, data = NULL, geom = "polygon",
position = "identity", na.rm = FALSE, myparam = "", show.legend = NA,
inherit.aes = TRUE, ...) {
layer(
stat = StatChull, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, myparam = myparam, ...)
)
}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_chull(fill = NA, colour = "black", myparam = "myval")这将打印在控制台上:
Warning: Ignoring unknown parameters: myparam
Error in message("My param has value ", myparam): argument "myparam" is missing, with no default有人能告诉我如何在compute_layer()中访问参数值吗?
发布于 2019-06-04 09:22:29
解释
所有geom_*()和stat_*()函数都是围绕layer()的包装器。如果我们检查其中的代码,我们可以看到与Stat关联的参数值是在stat_params中捕获的,这是通过stat$parameters(TRUE)获得的,其中stat引用了特定的Stat* ggproto对象:
> layer
function (geom = NULL, stat = NULL, data = NULL, mapping = NULL,
position = NULL, params = list(), inherit.aes = TRUE, check.aes = TRUE,
check.param = TRUE, show.legend = NA)
{
... # omitted
params <- rename_aes(params)
aes_params <- params[intersect(names(params), geom$aesthetics())]
geom_params <- params[intersect(names(params), geom$parameters(TRUE))]
stat_params <- params[intersect(names(params), stat$parameters(TRUE))]
all <- c(geom$parameters(TRUE), stat$parameters(TRUE), geom$aesthetics())
extra_param <- setdiff(names(params), all)
if (check.param && length(extra_param) > 0) {
warning("Ignoring unknown parameters: ", paste(extra_param,
collapse = ", "), call. = FALSE, immediate. = TRUE)
}
extra_aes <- setdiff(mapped_aesthetics(mapping), c(geom$aesthetics(),
stat$aesthetics()))
if (check.aes && length(extra_aes) > 0) {
warning("Ignoring unknown aesthetics: ", paste(extra_aes,
collapse = ", "), call. = FALSE, immediate. = TRUE)
}
ggproto("LayerInstance", Layer, geom = geom, geom_params = geom_params,
stat = stat, stat_params = stat_params, data = data,
mapping = mapping, aes_params = aes_params, position = position,
inherit.aes = inherit.aes, show.legend = show.legend)
}StatChull从Stat继承而不更改其参数函数,这将是:
> Stat$parameters
<ggproto method>
<Wrapper function>
function (...)
f(..., self = self)
<Inner function (f)>
function (self, extra = FALSE)
{
panel_args <- names(ggproto_formals(self$compute_panel))
group_args <- names(ggproto_formals(self$compute_group))
args <- if ("..." %in% panel_args)
group_args
else panel_args
args <- setdiff(args, names(ggproto_formals(Stat$compute_group)))
if (extra) {
args <- union(args, self$extra_params)
}
args
}解决方案
从上面可以看出,stat$parameters依赖于相关的Stat*的compute_panel / compute_group函数中列出的函数参数。
StatChull <- ggproto("StatChull",
Stat,
compute_layer = function (self, data, params, layout) {
message("My param has value ", params$myparam)
data[chull(data$x, data$y), , drop = FALSE]
},
compute_group = function(self, data, scales, na.rm, myparam) {
# this function is never triggered, but defined here
# in order for myparam to be included in stat_params
},
required_aes = c("x", "y")
)
# no change to stat_chull请注意,我们已经为compute_group定义了一个平凡的StatChull函数。此函数从未触发,因为compute_layer直接返回数据集(在Stat的正常情况下,compute_group由compute_panel触发,而后者又由compute_layer触发),但由于它确实将myparam作为其函数参数之一,所以myparam现在被认为是params中的参数值之一。
(您也可以通过定义一个简单的compute_panel函数来实现同样的结果。)
示范:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_chull(fill = NA, colour = "black", myparam = "myval")

My param has value myvalhttps://stackoverflow.com/questions/44550114
复制相似问题