"collapsing to unique 'x' values" 在这个例子中是什么意思?

What does "collapsing to unique 'x' values" mean in this example?

下面的示例图产生了关于

的警告
In regularize.values(x, y, ties, missing(ties), na.rm = na.rm) :
  collapsing to unique 'x' values

我无法弄清楚在我的示例中这意味着什么。

肯定与5有关,因为将5替换为41时警告消失。

df <- data.frame(x = 1, y = c(0, 0.25, 0.5, 0.75, 5))
ggplot2::ggplot(df, ggplot2::aes(x = x, y = y)) +
    ggplot2::geom_violin(draw_quantiles = c(0.5))

这是怎么回事?

@teunbrand 证实了我的假设

ecdf <- stats::approxfun(dens, data$y) 

(https://github.com/tidyverse/ggplot2/blob/cc3951cd942d/R/geom-violin.r#L200) 是罪魁祸首。

data$ydensity 中的零转化为累积密度 dens 中的相等值(“平局”)——因此警告。

可以通过 adjust 密度的带宽来避免这些零(这里,稍微 - 在我的示例中,我需要使用一个与 3 一样大的值):

df <- data.frame(x = 1, y = c(0, 0.25, 0.5, 0.75, 5))
ggplot2::ggplot(df, ggplot2::aes(x = x, y = y)) +
    ggplot2::geom_violin(draw_quantiles = c(0.5), adjust=1.1)

注意:由于 累积 密度使用 dens 等细节,代码难以阅读。

但是stats::regularize.values不一定更好:

    x <- xy.coords(x, y) # -> (x,y) numeric of same length
    y <- x$y
    x <- x$x

问题也可以通过

解决
ecdf <- stats::approxfun(dens, data$y, ties = "ordered") 

在这个猴子补丁中:

create_quantile_segment_frame <- function(data, draw_quantiles) {
  dens <- cumsum(data$density) / sum(data$density)
  ecdf <- stats::approxfun(dens, data$y, ties = "ordered")
  ys <- ecdf(draw_quantiles) # these are all the y-values for quantiles

  # Get the violin bounds for the requested quantiles.
  violin.xminvs <- (stats::approxfun(data$y, data$xminv))(ys)
  violin.xmaxvs <- (stats::approxfun(data$y, data$xmaxv))(ys)

  # We have two rows per segment drawn. Each segment gets its own group.
  ggplot2:::new_data_frame(list(
    x = ggplot2:::interleave(violin.xminvs, violin.xmaxvs),
    y = rep(ys, each = 2),
    group = rep(ys, each = 2)
  ))
}
assignInNamespace("create_quantile_segment_frame", create_quantile_segment_frame, "ggplot2")

df <- data.frame(x = 1, y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10))
ggplot2::ggplot(df, ggplot2::aes(x = x, y = y)) +
    ggplot2::geom_violin(draw_quantiles = c(0.5), bw = 0.1)