问题描述
我收到一条警告信息,我不明白ggplot2中的简单条形图
> df< - data.frame(X = 127:131,Y = rnorm(5))
> df
×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)
警告信息:
position_dodge需要固定宽度:输出可能不正确
它似乎只发生在某些X值范围内。我已经搜索了这方面的信息,但似乎都在谈论宽度真的不同的情况,或者stat不是身份的情况。在这种情况下,X值只是整数,所以它应该很简单。
生成的图表看起来不错,所以我对于忽略警告我感到不安,
任何想法是怎么回事? 设置,重新运行代码可以让我们发现问题。 $ b
在碰撞
函数(调用堆栈中的数字16),有这段代码:
if(!zero_range(range(widths )){
warning(name,requires constant width:output may be incorrect,
call。= FALSE)
}
意味着 widths
略有不同nt values。
format(widths,digits = 22)
#[1]0.90000000000000568434190.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
所以警告是错误的;不要担心。
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 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
It only seems to happen for certain ranges of X values. I've googled for info on this, but it all seems to be talking about cases when the widths genuinely are different, or cases where stat is not "identity". In this case the X values are just integers, so it should be simple.
The chart produced looks ok, so I'm uneasy about just ignoring a warning I don't understand.
Any idea what is going on?
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.
这篇关于为什么我会得到“position_dodge需要恒定的宽度”即使宽度在ggplot2中是不变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!