尝试在 ggplot2 中绘制简单的直方图会给出“错误:position_stack 需要恒定宽度”

发布于 2024-12-05 12:00:07 字数 656 浏览 0 评论 0原文

为什么会发生这种情况?我该如何解决这个问题?

> qplot(c(0,0,0,0,1e12))
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error: position_stack requires constant width
> qplot(c(0,0,0,0,1e12), binwidth=1e12/30)
Error: position_stack requires constant width
> qplot(c(0,0,0,0,1e12), binwidth=1e10)  # works
> qplot(c(0,0,0,0,1e12), binwidth=1e11)  # works

我发现 http://r.789695 .n4.nabble.com/ggplot2-histograms-a-subtle-error-found-td2305814.html但是在他们的例子中,问题是 binwidth 比值小得多。在这里,它们的数量级相似。

Why is this happening? And how do I work around this?

> qplot(c(0,0,0,0,1e12))
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error: position_stack requires constant width
> qplot(c(0,0,0,0,1e12), binwidth=1e12/30)
Error: position_stack requires constant width
> qplot(c(0,0,0,0,1e12), binwidth=1e10)  # works
> qplot(c(0,0,0,0,1e12), binwidth=1e11)  # works

I found http://r.789695.n4.nabble.com/ggplot2-histograms-a-subtle-error-found-td2305814.html but in their case the problem was that the binwidth is much smaller than the values. Here, they are on similar orders of magnitude.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2024-12-12 12:00:07

我要冒险猜测这是一个数值精度问题。使用 tracebackdebug 进行一些调查表明,问题发生在函数 collide 的检查中:

if (check.width && length(widths) > 1 && sd(widths) > 1e-06) {
            stop(name, " requires constant width", call. = FALSE)
}

宽度对于 sd 来说足够不同太大了。事实上,这个问题随着较小的值而消失,这似乎进一步证明了数值精度问题。

但是用于生成宽度的值并非源自碰撞,因此真正的问题可能是在更上游引起的,尽管我对 ggplot 的内部结构还不够熟悉> 进一步推测。

I'm going to go out on a limb here and guess that this is a numerical precision problem. A little sleuthing with traceback and debug reveals that the problem occurs in this check in the function collide:

if (check.width && length(widths) > 1 && sd(widths) > 1e-06) {
            stop(name, " requires constant width", call. = FALSE)
}

The widths are just different enough for the sd to be too big. The fact that this problem disappears with smaller values further seems like evidence for a numerical precision issue.

But the values used to generate the widths don't originate in collide, so the real problem is probably caused further upstream, though I'm not familiar enough with the innards of ggplot to speculate further.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文