尝试在 ggplot2 中绘制简单的直方图会给出“错误:position_stack 需要恒定宽度”
为什么会发生这种情况?我该如何解决这个问题?
> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要冒险猜测这是一个数值精度问题。使用
traceback
和debug
进行一些调查表明,问题发生在函数collide
的检查中:宽度对于 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
anddebug
reveals that the problem occurs in this check in the functioncollide
: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 ofggplot
to speculate further.