首页 > 解决方案 > 直方图超出范围

问题描述

我有下面的代码;

断点 <- seq(从 = 0,到 = 1500000,通过 = 10000)

hist(movies$Votes,breaks = breakPoints, main = "Votes distribution", col = "pink", xlab = "Votes")

我得到了错误:

hist.default 中的错误(movies$Votes,breaks = breakPoints,main = “Votes distribution”,:一些“x”未计算在内;也许“breaks”不跨越“x”的范围

在此处输入图像描述

标签: rhistogram

解决方案


首先,让我们通过使用变量 x 并为其分配一些值来创建一个可重现的示例。发生此错误是因为 'movies$Votes' 包含 0 到 1500000 范围之外的值。看看下面的例子。第一个运行良好,第二个给出错误(因为 -1 超出了我们指定的范围)。

# Values within range
x <- c(0:1500000)
breakPoints <- seq(from = 0, to = 1500000, by = 10000)
hist(x, breaks = breakPoints)

# Contains value ourside of range
x <- c(-1:1500000)
breakPoints <- seq(from = 0, to = 1500000, by = 10000)
hist(x, breaks = breakPoints) # Gives error

我建议运行下面的代码来了解您的数据跨越的范围。

range(movies$Votes)

如果您需要对数据应用限制,请查看以下问题:
Bound the values of a vector to a limit in R


推荐阅读