2

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

主要问题:

在我的函数内部calculate_shadows(),需要的参数params$anchorNULL. 我怎样才能访问它?

下面描述的目标仅用于学习如何创建自定义statgeom函数,这不是真正的目标:正如您从屏幕截图中看到的那样,我确实知道如何利用 的力量ggplot2来制作图表。

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

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

在此处输入图像描述

  1. stat读取数据,并且对于提供的变量("x", "y")shadows根据 的值进行计算stat。例如,通过传递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-qqor stat-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(),例如四分位数,以专注于基本要素。布局中有什么明显的错误吗?

几何阴影.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
}

.
4

1 回答 1

0

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

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

里面GeomShadows <- ggproto("GeomShadows", Geom,

也可能在里面StatShadows <- ggproto("StatShadows", Stat,

里面geom-.rstat-.r许多非常有用的注释,阐明了几何和统计数据是如何工作的。特别是(在 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"),
于 2018-12-30T17:59:22.217 回答