将额外参数传递给 ggplot2 中的自定义 geom

Passing an extra parameter to a custom geom in ggplot2

我正在创建一个自定义 geom 并希望它采用一个额外的参数,称为 showpoints,它对实际绘图有一些或其他的作用。例如,通过将其设置为 FALSE,geom 实际上是 returns a zeroGrob()。我找到了一种方法,但是 (i) 它很笨重而且有点奇怪 (ii) 我不完全理解我在做什么,这是一个不好的迹象。问题是当你定义一个新的统计数据时,可以 运行 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 和其他参数会自动 copied/inherited 来自该 geom,您不必手动指定所有这些参数。