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

Jan*_*ary 5 r ggplot2 ggproto

我正在创建一个自定义几何图形,并希望它带有一个名为 的额外参数,showpoints该参数对实际绘图执行某些操作或其他操作。例如,通过将其设置为 FALSE,geom 实际上会返回一个zeroGrob(). 我找到了一种方法来做到这一点,但是(i)它很笨重并且有点奇怪,并且(ii)我不完全理解我在做什么,这是一个坏兆头。问题是,当您定义新的统计数据时,可以运行setup_params,但 geoms 没有它:

与 Stat 和 Position 相比,Geom 略有不同,因为设置函数和计算函数的执行是分开的。setup_data 在位置调整之前运行,而 draw_layer 直到渲染时间才运行,晚得多。这意味着没有 setup_params,因为很难传达更改。

[来源]

基本上,我的代码的工作原理是,您可以使用附加参数来抑制点的绘制:

# 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)
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码,基于扩展 ggplot2 教程

## 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, ...)
  )
}
Run Code Online (Sandbox Code Playgroud)

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

teu*_*and 3

有一个相对简单的方法,就是将额外的参数作为参数传递给面板绘制函数。这是一个适用于您的简单示例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())
    }
  }
)
Run Code Online (Sandbox Code Playgroud)

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