将额外的参数传递给ggplot2中的自定义几何 [英] Passing an extra parameter to a custom geom in ggplot2

查看:103
本文介绍了将额外的参数传递给ggplot2中的自定义几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义几何图形,希望它采用一个名为showpoints的附加参数,该参数对实际绘图有其他作用.例如,通过将其设置为FALSE,geom实际上会返回zeroGrob().我找到了一种方法,但是(i)笨拙且有些怪异;(ii)我不完全了解自己在做什么,这是一个不好的信号.问题是,当您定义新的统计信息时,可以运行setup_params,但是geoms没有它:

I am creating a custom geom and would like it to take an extra parameter, called showpoints, which does something or other to the actual plot. For example, by setting it to FALSE, the geom actually returns a zeroGrob(). I have found a way of doing that, but (i) it is clunky and somewhat weird and (ii) I don't fully understand what I am doing, which is a bad sign. The problem is that when you are defining a new stat, it is possible to run setup_params, but geoms don't have it:

与统计和位置相比,Geom有所不同,因为 设置和计算功能的执行被拆分. setup_data 在位置调整之前运行,并且draw_layer直到运行 渲染时间,要晚得多.这意味着没有setup_params,因为 很难传达这些更改.

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.

[来源]

基本上,我拥有的代码在某种意义上可以正常工作,您可以使用附加参数来抑制绘制点:

Basically, the code I have works in the sense that you can use the additional parameter to suppress drawing of the points:

# 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)

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

Here is my code so far, based on 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,为什么不能以某种方式更轻松地访问它们?

Is there a simpler way of just passing a chosen extra param to the geom? If I can define extra_params, why can't I access them somehow more easily?

推荐答案

有一种相对简单的方法,那就是将多余的参数作为参数传递给面板绘制函数.这是一个可以与您的geom_simple_point()一起使用的简单示例:

There is a relatively easy way, and that is to pass the extra parameter as argument to the panel drawing function. Here is a simple example that would work with your 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复制/继承,而您不必手动指定所有这些参数.

As an aside, if you want to replicate behaviour of an already existing geom, such as geom_point(), it is easier to set your own ggproto object to inherit from that geom's ggproto object. That way, the default aes, required aes and other arguments that you specify in ggproto are automatically copied/inherited from that geom and you don't have to manually specify all of them.

这篇关于将额外的参数传递给ggplot2中的自定义几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆