hist() error: 'breaks' do not span range of 'data' and barplot(, log = "y") error: 'height + offset <= 0

13,902

original issue with hist:

I think you should set breaks breaks = seq(0, 700, by = 10), as you said in your comment, that range(data) reports c(0, 690).

hist.default() will report error, if the breaks does not cover the range of your data. Your current setting: breaks = seq(5, 700, by = 10) has minimum 5 and maximum 695, which is slightly insufficient at the left boundary.

I can easily reproduce the error you saw:

set.seed(0); x<-rexp(1000,1)
range(x)
# [1] 0.001700975 6.584532959
hist(x, breaks = seq(0.002, 7.002, by = 0.5))

Error in hist.default(x, breaks = seq(0.002, 7.002, by = 0.5)) : some 'x' not counted; maybe 'breaks' do not span range of 'x'

follow-up issue with barplot:

error log axis: at least one value 'height + offset <= 0

I can guess what happened. Check min(dh$counts), and I think it is 0. You know, log(0) = -INF, and you can't plot it.

Yes I have the 0 value. but why it works before I use seq(0, 700, by = 10)?

Why not have a check on foo <- hist(data, plot = FALSE), and see what bin cells and breaks it chooses? Chances are that hist() has wisely chosen bin size, so that no 0 count occurs.

When you try to refine the auto-chosen bin size, you should aim to achieve the same, not incurring 0. (Well, if you don't set log = "y" in barplot() then it really does not matter.)

I could also give you a trick/cheat. You might do barplot(dh$counts + 1, log = "y", ...). After adding 1, the minimum log value is 0, and there is no problem at all! Surely the counts values changes, but most likely it is not noticeable on your plot for presentation.

Share:
13,902
AbirH
Author by

AbirH

Updated on June 26, 2022

Comments

  • AbirH
    AbirH about 2 years

    In the x axis, I have a large amount of data (0:700) When I use this :

    dh <- hist(data)
    barplot(dh$counts, log="y", col="white", names.arg=dh$breaks[-1])
    

    I get this graphic:

    enter image description here

    But I want to get smaller values of breaks. I tried to modify the breaks value but I get an error

    Error in hist.default(data, breaks = seq(5, 700, by = 10)) : some 'data' not counted; maybe 'breaks' do not span range of 'data'


    follow up:

    After setting breaks = seq(0, 700, by = 10) as the answer suggests, hist() works fine but barplot() gives me error:

    error log axis: at least one value 'height + offset <= 0

    What happened?