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

前端 未结 1 1943
悲&欢浪女
悲&欢浪女 2021-02-12 18:11

I\'m getting a warning message I don\'t understand for simple bar charts in ggplot2

> df <- data.frame(X = 127:131, Y = rnorm(5))
> df
    X          Y
         


        
1条回答
  •  既然无缘
    2021-02-12 18:38

    Setting options(warn = 2, error = recover), and rerunning the code lets us find the problem.

    Inside the collide function (number 16 in the call stack), there is this piece of code:

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

    Floating point rounding errors mean that widths takes slightly different values.

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

    The tolerance for checking that the numbers are the same is too strict: about 2.2e-14.

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

    So the warning is erroneous; don't worry about it.

    0 讨论(0)
提交回复
热议问题