2

I am trying to create a histogram using Sturges' bining rule, yet I keep getting 2 errors, which probably have to do with the variable I am using, maybe because it has NA's?

Here is my process and the errors, what would you suggest?

k <- ggplot (world, aes (x=polstab))

wid <- ceiling ((max(world$polstab)- min (world$polstab))/ nclass.Sturges(world$polstab))

k + geom_histogram(col = "black", fill = "white", binwidth = wid)

Now, I get two error messages:

Removed 9 rows containing non-finite values (stat_bin).

and:

Computation failed in stat_bin(): missing value where TRUE/FALSE needed

The first one I understand, the second one is more problematic. What could I do to remove this error?

Uwe
  • 41,420
  • 11
  • 90
  • 134
elysa
  • 37
  • 1
  • 4
  • Hard to say without a reproducible example. What's `world`? – mtoto Oct 02 '16 at 13:52
  • Hello, world is a .dta file with lots of different variables, such as in this case political stability or corruption levels, hdi index etc. – elysa Oct 02 '16 at 13:57
  • Please share some data that will reproduce your issue. See: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – mtoto Oct 02 '16 at 14:04
  • The problem is that the data has 220 cases with lots of variables. I am quite new to RStudio and I am not very sure how much of the data is necessary to reproduce my issue. – elysa Oct 02 '16 at 15:24
  • 1
    If `polstab` does indeed have `NA` values, then `max(world$polstab)` and `min(world$polstab)` should each evaluate to `NA`, and as a result, `wid` would also evaluate to `NA`. You should do instead `wid <- ceiling ((max(world$polstab, na.rm = T)- min (world$polstab, na.rm = T))/ nclass.Sturges(world$polstab))`. Don't know if this would fix your problem without seeing the data. If there are many variables in your data, perhaps you can give us the output of `dput(world$polstab)`. This would give us the `polstab` variable in your `world` dataset. – jav Oct 02 '16 at 15:43
  • Thank you so much, it worked! – elysa Oct 02 '16 at 15:50
  • 1
    @Jav, can you move your solution from a comment to an answer? Elsa if the answer is satisfactory, please mark it as correct. – skoh Apr 30 '17 at 04:25

1 Answers1

0

If polstab does indeed have NA values, then max(world$polstab) and min(world$polstab) should each evaluate to NA, and as a result, wid would also evaluate to NA. You should do instead

wid <- ceiling ((max(world$polstab, na.rm = T)- min (world$polstab, na.rm = T))/ nclass.Sturges(world$polstab))
jav
  • 1,485
  • 8
  • 11