【问题标题】:How to I create a custom ggplot2 smoothing stat (not just a custom lm or glm model)如何创建自定义 ggplot2 平滑统计(不仅仅是自定义 lm 或 glm 模型)
【发布时间】:2019-02-22 10:41:36
【问题描述】:

我有一个使用移动窗口计算中位数和 90% CI 的函数。因此,对于每个x = seq(xmin, xmax, by = wStep),我返回所有y 的中位数以及5% 和95% 的分位数,其x 的值小于wSize/2。我想通过创建自定义平滑函数stat_movingwindow(),使用 ggplot2 将其显示为线条和功能区。我可以使用geom_smooth(data = ..., stat = "identity") 创建我想要的结果:

moveWin <- function(d, wSize = 0.5, wStep = 0.1, 
  f = function(x) quantile(x, prob = c(0.05,0.50,0.95), na.rm = TRUE)
){
  x <- seq(min(d$x), max(d$x), by = wStep)
  y <- matrix(NA, ncol = 3, nrow = length(x))
  for(i in seq_along(x)){
    y[i, ] <- f(d[abs(d$x - x[i]) < wSize/2, ]$y)
  }
  y <- as.tibble(y)
  colnames(y) <- c("ymin","y","ymax")
  y$x <- x
  return(as.tibble(y))
}

set.seed(123)
d <- tibble(
 x= sqrt(seq(0,1,length.out = 50))*10,
 y= rnorm(50)
)

ggplot(data = d) + aes(x = x, y = y) +
  geom_smooth(
    data    = function(d) moveWin(d, wSize = 1, wStep = 0.1), 
    mapping = aes(ymin = ymin, ymax= ymax),
    stat    = "identity") + 
  geom_point() + scale_x_continuous(breaks = 1:10)

按照 Vignette Extending ggplot2,这是我迄今为止提出的代码。但是,问题是这不显示功能区。也许我需要某种方式来声明这个自定义统计数据提供了美学 yminymax。如何获取以下代码以输出与上述类似的结果?

StatMovingWindow <- ggproto("StatMovingWindow", Stat,
  compute_group = function(data, scales, wSize, wStep, fun) {
    moveWin(data, wSize = wSize, wStep = wStep, f = fun)
  },

  required_aes = c("x", "y")
)
stat_movingwindow <- function(mapping = NULL, data = NULL, 
  fun = function(d) quantile(d, probs = c(0.05, 0.50, 0.95), na.rm = TRUE),
  wStep = 0.1, wSize = 1,
  geom = "smooth", position = "identity", show.legend = NA, inherit.aes = TRUE,
  ...
){
  layer(
    stat = StatMovingWindow, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(wStep = wStep, wSize = wSize, fun = fun, ...)
  )
}

ggplot(data = d) + aes(x = x, y = y) +
  stat_movingwindow(wStep = 0.1, wSize = 1) + 
  geom_point() + scale_x_continuous(breaks = 1:10)

【问题讨论】:

  • 尝试在stat_movingwindow()中添加se = TRUE
  • @Z.Lin 这工作 O.o ...但我不明白为什么。为什么 GeomSmooth 会找到这个参数?例如,如果我在moveWin 函数的定义中添加一个参数se=FALSE,如果我调用stat_movingwindow(..., se = TRUE),它不会设置为true。为什么它会得到例如wStep 的值?这两个参数都列在layer(... params= ...) 调用中?
  • 请参阅下面的冗长(-winded)解释。我不认为我可以在评论部分的范围内解释这一点......

标签: r ggplot2 smoothing


【解决方案1】:

stat_movingwindow 的代码中,对应geom 的行是geom = "smooth"

stat_movingwindow <- function(mapping = NULL, data = NULL, 
  fun = function(d) quantile(d, probs = c(0.05, 0.50, 0.95), na.rm = TRUE),
  wStep = 0.1, wSize = 1,
  geom = "smooth", # <- look here
  position = "identity", show.legend = NA, inherit.aes = TRUE,
  ...
){
  layer(
    stat = StatMovingWindow, data = data, mapping = mapping, geom = geom, 
    position = position, show.legend = show.legend, inherit.aes = inherit.aes,
    params = list(wStep = wStep, wSize = wSize, fun = fun, ...)
  )
}

检查geom_smooth的代码,我们看到它包含参数se = TRUE,并使用GeomSmooth作为它的geom:

> geom_smooth
function (mapping = NULL, data = NULL, stat = "smooth", position = "identity", 
    ..., method = "auto", formula = y ~ x, se = TRUE, # <- look here
    na.rm = FALSE, 
    show.legend = NA, inherit.aes = TRUE) 
{
    params <- list(na.rm = na.rm, se = se, ...)
    if (identical(stat, "smooth")) {
        params$method <- method
        params$formula <- formula
    }
    layer(data = data, mapping = mapping, stat = stat, geom = GeomSmooth, # <- and here
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = params)
}

深入研究 GeomSmooth,我们看到它的 draw_group 函数(负责绘制平滑线)将 se = FALSE 作为其默认参数。

从代码中,如果se == FALSEhas_ribbon 也将是FALSE,即使ymaxymin 都存在于您的数据中,这要感谢StatMovingWindow$compute_group 函数。这反过来意味着GeomLine$draw_panel(path, panel_params, coord) 的唯一结果将单独返回,而没有GeomRibbon$draw_group(ribbon, panel_params, coord)

> GeomSmooth$draw_group
<ggproto method>
  <Wrapper function>
    function (...) 
f(...)

  <Inner function (f)>
    function (data, panel_params, coord, se = FALSE) # <- look here
{
    ribbon <- transform(data, colour = NA)
    path <- transform(data, alpha = NA)
    has_ribbon <- se && !is.null(data$ymax) && !is.null(data$ymin) # <- and here
    gList(if (has_ribbon) GeomRibbon$draw_group(ribbon, panel_params, coord), 
          GeomLine$draw_panel(path, panel_params, coord))
}

简而言之,geom_smooth 的默认参数se = TRUE 覆盖了GeomSmooth$draw_group 中的默认行为,(stat_smooth 也是如此)如果我们愿意,我们应该在stat_movingwindow 中做同样的事情达到同样的效果。

如果您通常希望绘制功能区,您可以在stat_movingwindow 的定义中包含se = TRUE 作为参数。如果它是临时的,您可以在需要时将其包含在您的代码中。

【讨论】:

    猜你喜欢
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多