为什么我得到“position_dodge需要不断宽度”即使宽度在不断GGPLOT2(Why do

2019-08-07 22:52发布

我收到一条警告消息,我不为她在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了关于这个信息,但是这一切似乎在谈论当真正的宽度不同的情况,或情况的统计是不是“身份”。 在这种情况下,X值只是整数,所以它应该是简单的。

制作图表看起来不错,所以我很不安只是忽略了一个警告,我不明白。

任何想法是怎么回事?

Answer 1:

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

内的collide功能(在调用栈号16),有这一段代码:

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

因此,警告是错误的; 不用担心这个。



文章来源: Why do i get “position_dodge requires constant width” even though widths are constant in ggplot2
标签: r plot ggplot2