【问题标题】:Extending ggplot2: How to build a geom and stat?扩展 ggplot2:如何构建 geom 和 stat?
【发布时间】:2018-12-26 11:09:30
【问题描述】:

我正处于学习如何扩展ggplot2 的早期阶段。我想创建一个自定义geom 和关联的stat。我的出发点是vignette。此外,我还受益于thisthis。我正在尝试制作一个模板来教自己,并希望能教给其他人。

主要问题:

在我的函数calculate_shadows() 中,所需的参数params$anchorNULL。如何访问它?

下面描述的目标仅用于学习如何创建自定义 statgeom 函数,这不是一个真正的目标:从屏幕截图中可以看出,我确实知道如何利用 @987654336 的力量@ 制作图表。

  1. geom 将读取数据,并且对于提供的变量 ("x", "y") 将绘制(因为需要更好的词)shadows:水平线 min(x)--max(x) 在默认 y=0 和垂直线 @ 987654342@ 默认为x=0。如果提供了选项,则可以更改这些“锚点”,例如如果用户提供x = 35, y = 1,则将在截距y = 1 处绘制水平线,而在截距x = 35 处绘制垂直线。用法:

    library(ggplot2)
    ggplot(data = mtcars, aes(x = mpg, y = wt)) + 
        geom_point() +
        geom_shadows(x = 35, y = 1) 
    

  1. stat 将读取数据,对于提供的变量,("x", "y") 将根据stat 的值计算shadows。例如,通过传递stat = "identity",将为数据的最小值和最大值计算阴影(由geom_shadows 完成)。但是通过stat = "quartile",将计算第一和第三四分位数的阴影。更一般地说,可以传递一个像 stats::quantile 这样的函数,参数为 args = list(probs = c(0.10, 0.90), type = 6),以使用第 10 和第 90 个百分位数以及类型 6 的分位数方法来计算阴影。用法:

    ggplot(data = mtcars, aes(x = mpg, y = wt)) + 
        geom_point() +
        stat_shadows(stat = "quartile") 
    

不幸的是,我对扩展 ggplot2 不熟悉,这使我无法实现目标。这些图是用geom_segment“伪造”的。基于上面引用的教程和讨论并检查现有代码(如stat-qqstat-smooth),我已经为这个目标构建了一个基本架构。一定有很多错误,不胜感激。另外,请注意,这些方法中的任何一种都可以:geom_shadows(anchor = c(35, 1))geom_shadows(x = 35, y = 1)

现在这是我的努力。首先,geom-shadows.r 定义geom_shadows()。二、stat-shadows.r定义stat_shadows()。代码不能按原样工作。但如果我执行它的内容,它确实会产生所需的统计数据。为了清楚起见,我已经删除了stat_shadows() 中的大部分计算,例如四分位数,以专注于要领。布局有什么明显的错误吗?

geom-shadows.r

#' documentation ought to be here
geom_shadows <- function(
  mapping = NULL, 
  data = NULL, 
  stat = "shadows", 
  position = "identity", 
  ...,
  anchor = list(x = 0, y = 0),
  shadows = list("x", "y"), 
  type = NULL,
  na.rm = FALSE,
  show.legend = NA, 
  inherit.aes = TRUE) {
    layer(
      data = data,
      mapping = mapping,
      stat = stat,
      geom = GeomShadows,
      position = position,
      show.legend = show.legend,
      inherit.aes = inherit.aes,
      params = list(
        anchor = anchor,
        shadows = shadows,
        type = type,  
        na.rm = na.rm,
        ...
    )
  )
}

GeomShadows <- ggproto("GeomShadows", Geom, 

  # set up the data, e.g. remove missing data
  setup_data = function(data, params) { 
    data 
  }, 

  # set up the parameters, e.g. supply warnings for incorrect input
  setup_params = function(data, params) {
    params
  },

  draw_group = function(data, panel_params, coord, anchor, shadows, type) { 
    # draw_group uses stats returned by compute_group

    # set common aesthetics
    geom_aes <- list(
      alpha = data$alpha,
      colour = data$color,
      size = data$size,
      linetype = data$linetype,
      fill = alpha(data$fill, data$alpha),
      group = data$group
    )

    # merge aesthetics with data calculated in setup_data
    geom_stats <- new_data_frame(c(list(
          x = c(data$x.xmin, data$y.xmin),
          xend = c(data$x.xmax, data$y.xmax),
          y = c(data$x.ymin, data$y.ymin),
          yend = c(data$x.ymax, data$y.ymax),
          alpha = c(data$alpha, data$alpha) 
        ), geom_aes
      ), n = 2) 

    # turn the stats data into a GeomPath
    geom_grob <- GeomSegment$draw_panel(unique(geom_stats), 
        panel_params, coord) 

    # pass the GeomPath to grobTree
    ggname("geom_shadows", grobTree(geom_grob)) 
  },

  # set legend box styles
  draw_key = draw_key_path,

  # set default aesthetics 
  default_aes = aes(
    colour = "blue",
    fill = "red",
    size = 1,
    linetype = 1,
    alpha = 1
  )

)

stat-shadows.r

#' documentation ought to be here
stat_shadows <-  
  function(mapping = NULL, 
           data = NULL,
           geom = "shadows", 
           position = "identity",
           ...,
           # do I need to add the geom_shadows arguments here?
           anchor = list(x = 0, y = 0),
           shadows = list("x", "y"), 
           type = NULL,
           na.rm = FALSE,
           show.legend = NA,
           inherit.aes = TRUE) {
  layer(
    stat = StatShadows,  
    data = data,
    mapping = mapping,
    geom = geom,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      # geom_shadows argument repeated here?
      anchor = anchor,  
      shadows = shadows,
      type = type,
      na.rm = na.rm,
      ...
    )
  )
}

StatShadows <- 
  ggproto("StatShadows", Stat,

    # do I need to repeat required_aes?
    required_aes = c("x", "y"), 

    # set up the data, e.g. remove missing data
    setup_data = function(data, params) {
      data
    },

    # set up parameters, e.g. unpack from list
    setup_params = function(data, params) {
      params
    },

    # calculate shadows: returns data_frame with colnames: xmin, xmax, ymin, ymax 
    compute_group = function(data, scales, anchor = list(x = 0, y = 0), shadows = list("x", "y"), type = NULL, na.rm = TRUE) {

      .compute_shadows(data = data, anchor = anchor, shadows = shadows, type = type)

  }
)

# Calculate the shadows for each type / shadows / anchor
.compute_shadows <- function(data, anchor, shadows, type) {

# Deleted all type-checking, etc. for MWE
# Only 'type = c(double, double)' accepted, e.g. type = c(0, 1)

qs <- type

# compute shadows along the x-axis
if (any(shadows == "x")) {
    shadows.x <- c(
    xmin = as.numeric(stats::quantile(data[, "x"], qs[[1]])),
    xmax = as.numeric(stats::quantile(data[, "x"], qs[[2]])),
    ymin = anchor[["y"]], 
    ymax = anchor[["y"]]) 
}

# compute shadows along the y-axis
if (any(shadows == "y")) {
    shadows.y <- c(
    xmin = anchor[["x"]], 
    xmax = anchor[["x"]], 
    ymin = as.numeric(stats::quantile(data[, "y"], qs[[1]])),
    ymax = as.numeric(stats::quantile(data[, "y"], qs[[2]])))
} 

# store shadows in one data_frame
stats <- new_data_frame(c(x = shadows.x, y = shadows.y))

# return the statistics
stats
}

.

【问题讨论】:

  • 这个问题被编辑以反映最重要的问题:特别是,我修复了draw_group在合并美学与数据时的明显问题。代码主要基于geom_boxplotstat_boxplot,尤其是whiskers
  • 请注意,要编译上面的代码,您需要在特殊函数前面加上 ggplot2:: 并复制 new_data_frame 之类的函数,或者(这就是我所做的)克隆 ggplot2 包并在NAMESPACEDESCRIPTION 中添加并正确引用上述两个文件进行编译

标签: r ggplot2 proto ggproto


【解决方案1】:

直到一个更彻底的答案出现:你失踪了

extra_params = c("na.rm", "shadows", "anchor", "type"),

GeomShadows &lt;- ggproto("GeomShadows", Geom,

也可能在StatShadows &lt;- ggproto("StatShadows", Stat, 内。

geom-.rstat-.r 内部有许多非常有用的cmets,它们阐明了geoms 和stats 的工作原理。特别是(在 github 问题上向 Claus Wilke 提示):

# Most parameters for the geom are taken automatically from draw_panel() or
# draw_groups(). However, some additional parameters may be needed
# for setup_data() or handle_na(). These can not be imputed automatically,
# so the slightly hacky "extra_params" field is used instead. By
# default it contains `na.rm`
extra_params = c("na.rm"),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2020-10-16
    • 2018-12-25
    • 2021-01-12
    • 2012-05-08
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多