r - 如何创建自定义 ggplot2 平滑统计(不仅仅是自定义 lm 或 glm 模型)

标签 r ggplot2 smoothing

我有一个函数可以使用移动窗口计算中位数和 90% 置信区间。因此,对于每个 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)

ggplot with moving window smoothing

按照小插图 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)

custom smoothing stat does not show a ribbon

最佳答案

在您的 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,即使 ymax > & ymin 存在于您的数据中,感谢 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 中执行相同的操作。

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

关于r - 如何创建自定义 ggplot2 平滑统计(不仅仅是自定义 lm 或 glm 模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54825289/

相关文章:

r - 在 R 中用循环和 if else 解算 SIR 模型

r - 如何用ggplot对齐两个图?

html - 在每个 div 具有相同类的情况下扩展 div 的高度

c# - 如何绘制平滑/圆角/曲线图? (C#)

python - 减少数据噪音

r - 我怎样才能用 R 的 `:` 运算符来满足我的困境?

R寓言::model() "turn on"进度条

r - 带有对象的管道操作 dplyr R 返回了一个列表

r - ggplot : stacked barplot in reverse order

r - 将 ggplot2 和 facet_grid 一起用于连续变量和分类变量 (R)