gpt4 book ai didi

r - 将额外参数传递给 ggplot2 中的自定义几何图形

转载 作者:行者123 更新时间:2023-12-04 12:40:01 24 4
gpt4 key购买 nike

我正在创建一个自定义 geom 并希望它采用一个额外的参数,称为 showpoints ,它对实际情节做某事或其他。例如,通过将其设置为 FALSE,geom 实际上返回一个 zeroGrob() .我找到了一种方法来做到这一点,但是 (i) 它笨拙而且有点奇怪,并且 (ii) 我不完全理解我在做什么,这是一个不好的迹象。问题是当你定义一个新的 stat 时,可以运行 setup_params ,但 geoms 没有它:

Compared to Stat and Position, Geom is a little different because the execution of the setup and compute functions is split up. setup_data runs before position adjustments, and draw_layer is not run until render time, much later. This means there is no setup_params because it's hard to communicate the changes.



[Source]

基本上,我的代码在您可以使用附加参数来抑制绘制点的意义上起作用:
# draw the points by default
ggplot(mpg, aes(displ, hwy)) + geom_simple_point()

# suppresses drawing of the points
ggplot(mpg, aes(displ, hwy)) + geom_simple_point(showpoints=FALSE)

到目前为止,这是我的代码,基于 extending ggplot2 tutorial :
## Return the grob to draw. The additional parameter,
## showpoints, determines whether a points grob should be returned,
## or whether zeroGrob() should take care of not showing the points
.draw_panel_func <- function(data, panel_params, coord) {
coords <- coord$transform(data, panel_params)
showpoints <- unique(data$showpoints)
cat("showpoints=", as.character(showpoints), "\n")
if(!is.null(showpoints) && is.logical(showpoints) && !showpoints) {
return(zeroGrob())
} else {
return(
grid::pointsGrob(coords$x, coords$y,
pch = coords$shape,
gp = grid::gpar(col = coords$colour))
)
}
}

## definition of the new geom. setup_data inserts the parameter
## into data, therefore making it accessible for .draw_panel_func
GeomSimplePoint <- ggproto("GeomSimplePoint", Geom,
required_aes = c("x", "y"),
default_aes = aes(shape = 19, colour = "black"),
draw_key = draw_key_point,
setup_data = function(data, params) {
if(!is.null(params$showpoints)) {
data$showpoints <- params$showpoints
}
data
},
extra_params = c("na.rm", "showpoints"),
draw_panel = .draw_panel_func
)

geom_simple_point <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, showpoints=TRUE, ...) {
layer(
geom = GeomSimplePoint, mapping = mapping, data = data, stat = stat,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, showpoints=showpoints, ...)
)
}

是否有一种更简单的方法可以将选定的额外参数传递给 geom?如果我可以定义 extra_params ,为什么我不能以某种方式更轻松地访问它们?

最佳答案

有一个比较简单的方法,就是将额外的参数作为参数传递给面板绘制函数。这是一个简单的例子,适用于您的 geom_simple_point() :

GeomSimplePoint <- ggproto(
"GeomSimplePoint",
GeomPoint,
extra_params = c("na.rm", "showpoints"),
draw_panel = function(data, panel_params,
coord, na.rm = FALSE, showpoints = TRUE) {
if (showpoints) {
return(GeomPoint$draw_panel(data, panel_params, coord, na.rm = na.rm))
} else {
return(zeroGrob())
}
}
)

顺便说一句,如果您想复制已经存在的 geom 的行为,例如 geom_point() ,更容易设置您自己的 ggproto 对象以从该 geom 的 ggproto 对象继承。这样,您在 ggproto 中指定的默认 aes、必需 aes 和其他参数会自动从该 geom 复制/继承,您不必手动指定所有这些参数。

关于r - 将额外参数传递给 ggplot2 中的自定义几何图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57072096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com