user10080061
user10080061

Reputation:

histogram breaks out of range

I have the code below;

breakPoints <- seq(from = 0, to = 1500000, by = 10000)

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

and I get the error:

Error in hist.default(movies$Votes, breaks = breakPoints, main = "Votes distribution", : some 'x' not counted; maybe 'breaks' do not span range of 'x'

enter image description here

Upvotes: 0

Views: 368

Answers (1)

MatAff
MatAff

Reputation: 1331

Firstly, let's create a reproducible example by using variable x and assigning some values to it. This error happens because 'movies$Votes' contains values outside the 0 to 1500000 range. Have a look at the examples below. The first one runs fine, the second gives an error (as -1 fall outside of our specified range).

# 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

I would recommend running the code below to get a sense of the range your data spans.

range(movies$Votes)

If you need to apply a limit to your data, have a look at this question:
Bound the values of a vector to a limit in R

Upvotes: 0

Related Questions