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

时间:2013-01-23 10:04:19

标签: r plot ggplot2

我收到一条警告信息,我不明白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值范围。我已经用Google搜索了这方面的信息,但这一切似乎都在谈论宽度确实不同的情况,或者stat不是“身份”的情况。在这种情况下,X值只是整数,所以它应该很简单。

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

知道发生了什么事吗?

1 个答案:

答案 0 :(得分:38)

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

collide函数(调用堆栈中的数字16)中,有一段代码:

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

Floating point rounding errors表示widths的值略有不同。

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

用于检查数字是否相同的tolerance过于严格:约为2.2e-14。

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

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