Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale_fill_binned throws error if all bins have the same value #5066

Open
sebneus opened this issue Nov 29, 2022 · 1 comment
Open

scale_fill_binned throws error if all bins have the same value #5066

sebneus opened this issue Nov 29, 2022 · 1 comment

Comments

@sebneus
Copy link

sebneus commented Nov 29, 2022

I want to print a couple of ggplots in a group_walk loop. But one of the plots only returns values in one bin which resolves in an error.

Here is an example:

library(ggplot2)

data.frame(x = c("a", "b"), 
           y = c("A", "B")) |> 
  ggplot() + 
  geom_bin_2d(aes(x = x, y = y)) + 
  scale_fill_binned()
#> Error in cut.default(x, breaks, labels = FALSE, include.lowest = TRUE, : invalid number of intervals

Created on 2022-11-29 with reprex v2.0.2

If there is a bin with a at least two different numbers (here 1 and 2), the plot succeeds:

library(ggplot2)

data.frame(x = c("a", "b", "b"), 
           y = c("A", "B", "B")) |> 
  ggplot() + 
  geom_bin_2d(aes(x = x, y = y)) + 
  scale_fill_binned()

Created on 2022-11-29 with reprex v2.0.2

If I remove the scale_fill_binned() part the chart is drawn, but with no color steps. Similar (but different) error is thrown if I use guides(fill = "colorsteps"). Using geom_tile(aes(x, y, fill = after_stat(count)), stat = "bin2d") shows the same behaviour.

ggplot version is 3.4.0.

Regards
SebNeu

@jan-glx
Copy link
Contributor

jan-glx commented Aug 3, 2023

Unfortuneatly @teunbrand 's fix does not quite cut 😛 it for scale_x_binned. See also this example:

library(ggplot2)
ggplot(data.frame(x=1), aes(x=x)) + geom_bar() + scale_x_binned(limits = c(0,1))

ggplot(data.frame(x=1), aes(x=x)) + geom_bar() + scale_x_binned()
#> Error in cut.default(x, all_breaks, labels = FALSE, include.lowest = TRUE, : invalid number of intervals

Created on 2023-08-03 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.0 (2021-05-18)
#>  os       CentOS Linux 7 (Core)
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_US.UTF-8
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Berlin
#>  date     2023-08-03
#>  pandoc   2.19.2 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.1.0)
#>  colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.1.0)
#>  curl          4.3.2      2021-06-23 [1] CRAN (R 4.1.0)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.0)
#>  dplyr         1.1.2      2023-04-20 [1] CRAN (R 4.1.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         1.0.2      2022-01-14 [1] CRAN (R 4.1.0)
#>  farver        2.1.0      2021-02-28 [1] CRAN (R 4.1.0)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.0)
#>  generics      0.1.2      2022-01-31 [1] CRAN (R 4.1.0)
#>  ggplot2     * 3.4.2.9000 2023-08-03 [1] Github (tidyverse/ggplot2@cd7199d)
#>  glue          1.6.2      2022-02-24 [1] CRAN (R 4.1.0)
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.1.0)
#>  httr          1.4.2      2020-07-20 [1] CRAN (R 4.1.0)
#>  knitr         1.37       2021-12-16 [1] CRAN (R 4.1.0)
#>  labeling      0.4.2      2020-10-20 [1] CRAN (R 4.1.0)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.1.0)
#>  magrittr      2.0.2      2022-01-26 [1] CRAN (R 4.1.0)
#>  mime          0.12       2021-09-28 [1] CRAN (R 4.1.0)
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.1.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
#>  purrr         1.0.1      2023-01-10 [1] CRAN (R 4.1.0)
#>  R.cache       0.16.0     2022-07-21 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.1.0)
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.0)
#>  rlang         1.1.1      2023-04-28 [1] CRAN (R 4.1.0)
#>  rmarkdown     2.23       2023-07-01 [1] CRAN (R 4.1.0)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  scales        1.2.1      2022-08-20 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.0)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.0)
#>  stringr       1.5.0      2022-12-02 [1] CRAN (R 4.1.0)
#>  styler        1.10.1     2023-06-05 [1] CRAN (R 4.1.0)
#>  tibble        3.2.1      2023-03-20 [1] CRAN (R 4.1.0)
#>  tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.1.0)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.6.2      2023-04-19 [1] CRAN (R 4.1.0)
#>  withr         2.5.0      2022-03-03 [1] CRAN (R 4.1.0)
#>  xfun          0.39       2023-04-20 [1] CRAN (R 4.1.0)
#>  xml2          1.3.3      2021-11-30 [1] CRAN (R 4.1.0)
#>  yaml          2.3.4      2022-02-17 [1] CRAN (R 4.1.0)
#> 
#>  [1] /home/gleixner/R/x86_64-pc-linux-gnu-library/4.1.0d
#>  [2] /software/r/4.1.0/lib64/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
So as a workaround you can specify limits for the binned scale yourself.

I am afraid this issue isn't too much an implementation problem but rather a design question. It is not clear to me how a binned scale should behave without predefined breaks, but I am under the impression that in many cases we don't actually want a scale_x_binned but a scale_x_integer or scale_x_binned_integer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants