为什么即使 ggplot2 中的宽度是恒定的,我也会得到“position_dodge需要恒定的宽度”

Why do i get "position_dodge requires constant width" even though widths are constant in ggplot2

提问人:Corvus 提问时间:1/23/2013 更新时间:3/31/2015 访问量:6740

问:

我收到一条警告消息,我不明白 ggplot2 中的简单条形图

> df <- data.frame(X = 127:131, Y = rnorm(5))
> df
    X          Y
1 127  0.9391077
2 128 -0.9392529
3 129 -1.1296221
4 130  1.1454907
5 131  1.8564596
> ggplot(df) + geom_bar(aes(X,Y), stat ="identity", position = "dodge")
Warning message:
position_dodge requires constant width: output may be incorrect 

它似乎只发生在某些范围的 X 值上。我已经在谷歌上搜索了这方面的信息,但这一切似乎都在谈论宽度真正不同的情况,或者统计数据不是“身份”的情况。在这种情况下,X 值只是整数,因此它应该很简单。

生成的图表看起来不错,所以我对忽略我不理解的警告感到不安。

知道这是怎么回事吗?

r ggplot2

评论

4赞 Richie Cotton 1/23/2013
我在 github 上提交了一个关于狡猾警告的问题。github.com/hadley/scales/issues/33
1赞 Richie Cotton 6/11/2015
此问题现已在 的开发版本中修复。scales

答:

39赞 Richie Cotton 1/23/2013 #1

设置选项(warn = 2, error = recover),然后重新运行代码可以让我们找到问题所在。

在函数内部(调用堆栈中的第 16 个),有一段代码:collide

if (!zero_range(range(widths))) {
    warning(name, " requires constant width: output may be incorrect", 
        call. = FALSE)
}

浮点舍入误差意味着采用的值略有不同。widths

format(widths, digits = 22)
# [1] "0.9000000000000056843419" "0.8999999999999914734872" "0.8999999999999772626325"

检查数字是否相同的公差过于严格:大约 2.2e-14。

args(zero_range)
# function (x, tol = .Machine$double.eps * 100) 
# NULL
.Machine$double.eps * 100
# [1] 2.220446e-14

所以警告是错误的;别担心。