eclarke / ggbeeswarm

Column scatter / beeswarm-style plots in ggplot2
GNU General Public License v3.0
531 stars 31 forks source link

`geom_beeswarm()` gives error with log of zero #87

Open billdenney opened 1 year ago

billdenney commented 1 year ago

geom_beeswarm() is very useful, thanks for making it!

When I use it with a log transform and data including zeros, it fails. With other geoms, this would result in a point sitting on the axis (the behavior I would desire/expect). Can this be modified?

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3
library(ggbeeswarm)

d_plot <- data.frame(A = c(0, 1), B = "B")

ggplot(d_plot, aes(x = B, y = A)) +
  geom_beeswarm()


ggplot(d_plot, aes(x = B, y = A)) +
  geom_beeswarm() +
  scale_y_log10()
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Error in `geom_beeswarm()`:
#> ! Problem while computing position.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `.calculateSwarmUsingC()`:
#> ! NA/NaN/Inf in foreign function call (arg 1)
#> Backtrace:
#>      ▆
#>   1. ├─base::tryCatch(...)
#>   2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   3. │   ├─base (local) tryCatchOne(...)
#>   4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#>   6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   8. ├─base::withCallingHandlers(...)
#>   9. ├─base::saveRDS(...)
#>  10. ├─base::do.call(...)
#>  11. ├─base (local) `<fn>`(...)
#>  12. ├─global `<fn>`(input = base::quote("woody-kiwi_reprex.R"))
#>  13. │ └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#>  14. │   └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#>  15. │     └─knitr:::process_file(text, output)
#>  16. │       ├─base::withCallingHandlers(...)
#>  17. │       ├─knitr:::process_group(group)
#>  18. │       └─knitr:::process_group.block(group)
#>  19. │         └─knitr:::call_block(x)
#>  20. │           └─knitr:::block_exec(params)
#>  21. │             └─knitr:::eng_r(options)
#>  22. │               ├─knitr:::in_input_dir(...)
#>  23. │               │ └─knitr:::in_dir(input_dir(), expr)
#>  24. │               └─knitr (local) evaluate(...)
#>  25. │                 └─evaluate::evaluate(...)
#>  26. │                   └─evaluate:::evaluate_call(...)
#>  27. │                     ├─evaluate (local) handle(...)
#>  28. │                     │ └─base::try(f, silent = TRUE)
#>  29. │                     │   └─base::tryCatch(...)
#>  30. │                     │     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  31. │                     │       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  32. │                     │         └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  33. │                     ├─base::withCallingHandlers(...)
#>  34. │                     ├─base::withVisible(value_fun(ev$value, ev$visible))
#>  35. │                     └─knitr (local) value_fun(ev$value, ev$visible)
#>  36. │                       └─knitr (local) fun(x, options = options)
#>  37. │                         ├─base::withVisible(knit_print(x, ...))
#>  38. │                         ├─knitr::knit_print(x, ...)
#>  39. │                         └─knitr:::knit_print.default(x, ...)
#>  40. │                           └─evaluate (local) normal_print(x)
#>  41. │                             ├─base::print(x)
#>  42. │                             └─ggplot2:::print.ggplot(x)
#>  43. │                               ├─ggplot2::ggplot_build(x)
#>  44. │                               └─ggplot2:::ggplot_build.ggplot(x)
#>  45. │                                 └─ggplot2:::by_layer(...)
#>  46. │                                   ├─rlang::try_fetch(...)
#>  47. │                                   │ ├─base::tryCatch(...)
#>  48. │                                   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  49. │                                   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  50. │                                   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  51. │                                   │ └─base::withCallingHandlers(...)
#>  52. │                                   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  53. │                                     └─l$compute_position(d, layout)
#>  54. │                                       └─ggplot2 (local) compute_position(..., self = self)
#>  55. │                                         └─self$position$compute_layer(data, params, layout)
#>  56. │                                           └─ggplot2 (local) compute_layer(..., self = self)
#>  57. │                                             └─ggplot2:::dapply(...)
#>  58. │                                               └─ggplot2 (local) apply_fun(df)
#>  59. │                                                 └─ggplot2 (local) fun(x, ...)
#>  60. │                                                   └─self$compute_panel(data = data, params = params, scales = scales)
#>  61. │                                                     └─ggbeeswarm (local) compute_panel(...)
#>  62. │                                                       └─base::lapply(...)
#>  63. │                                                         └─ggbeeswarm (local) FUN(X[[i]], ...)
#>  64. │                                                           └─beeswarm::swarmx(...)
#>  65. │                                                             └─beeswarm:::.calculateSwarmUsingC(...)
#>  66. └─base::.handleSimpleError(...)
#>  67.   └─rlang (local) h(simpleError(msg, call))
#>  68.     └─handlers[[1L]](cnd)
#>  69.       └─cli::cli_abort(...)
#>  70.         └─rlang::abort(...)

Created on 2023-05-19 with reprex v2.0.2

aroneklund commented 1 year ago

Perhaps a solution could be to add an option to beeswarm that allows it to jitter +Inf and -Inf independently from the finite values?

beeswarm(..., allow_infinite = TRUE)

.. and I imagine that ggbeeswarm would use this option by default, and that this would give the behavior (infinite values peeking out from behind the plot borders) that ggplot2 users expect?

billdenney commented 1 year ago

@aroneklund , that makes sense as a possibility. I'm going to move the conversation to the beeswarm PR.