ggplot 'non-finite values' error
Asked Answered
G

1

18

I have an R dataframe (df) that looks like this:

blogger; word; n; total
joe; dorothy; 17; 718
paul; sheriff; 10; 354
joe; gray; 9; 718
joe; toto; 9; 718
mick; robin; 9; 607
paul; robin; 9; 354
...

I want to use ggplot2 to plot n divided by total for each blogger.

I have this code:

ggplot(df, aes(n/total, fill = blogger)) +
  geom_histogram(show.legend = FALSE) +
  xlim(NA, 0.0004) +
  facet_wrap(~blogger, ncol = 2, scales = "free_y")

But it yields this warning:

Warning message:
“Removed 1474 rows containing non-finite values (stat_bin).”Warning message in rep(no, length.out = length(ans)):
“'x' is NULL so the result will be NULL”
Gantz answered 18/4, 2017 at 15:2 Comment(2)
Non finite suggests a division by zeroValladolid
It is because the upper end of your range, 0.0004, is less than many (perhaps all) of your values of n/totalMikes
B
23

In the example plot here that you're working from, there are very long tails at higher n / total, and thus the use of xlim(). Try making your plot without any change to the limits of the x-axis; you might not need to tweak that at all in your case.

ggplot(df, aes(n/total, fill = blogger)) +
  geom_histogram(show.legend = FALSE) +
  facet_wrap(~blogger, ncol = 2, scales = "free_y")
Belike answered 21/4, 2017 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.