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

Error when setting breaks in coord_radial() #6271

Open
Yunuuuu opened this issue Jan 6, 2025 · 6 comments · May be fixed by #6272
Open

Error when setting breaks in coord_radial() #6271

Yunuuuu opened this issue Jan 6, 2025 · 6 comments · May be fixed by #6272
Labels
bug an unexpected problem or unintended behavior coord 🗺️ guides 📏

Comments

@Yunuuuu
Copy link
Contributor

Yunuuuu commented Jan 6, 2025

library(ggplot2)
data <- reshape2::melt(matrix(rnorm(100), 10))
ggplot(data, aes(Var1, Var2)) +
    geom_tile(aes(fill = value)) +
    scale_fill_viridis_c() +
    scale_x_discrete(breaks = 1:2) +
    coord_radial()
#> Error in switch(params$position, top = , right = , theta.sec = -Inf, Inf): EXPR must be a length 1 vector

Created on 2025-01-07 with reprex v2.1.0

I have using the development version of ggplot2.

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       Ubuntu 24.04.1 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       Asia/Shanghai
#>  date     2025-01-07
#>  pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  cli            3.6.3      2024-06-21 [1] CRAN (R 4.4.0)
#>  digest         0.6.37     2024-08-19 [1] CRAN (R 4.4.2)
#>  dplyr          1.1.4      2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate       1.0.1      2024-10-10 [1] CRAN (R 4.4.2)
#>  fansi          1.0.6      2023-12-08 [1] CRAN (R 4.4.0)
#>  farver         2.1.2      2024-05-13 [1] CRAN (R 4.4.0)
#>  fastmap        1.2.0      2024-05-15 [1] CRAN (R 4.4.0)
#>  fs             1.6.5      2024-10-30 [1] CRAN (R 4.4.2)
#>  generics       0.1.3      2022-07-05 [1] CRAN (R 4.4.0)
#>  ggplot2      * 3.5.1.9000 2025-01-06 [1] Github (tidyverse/ggplot2@efc53cc)
#>  glue           1.8.0      2024-09-30 [1] CRAN (R 4.4.2)
#>  gtable         0.3.6      2024-10-25 [1] CRAN (R 4.4.2)
#>  htmltools      0.5.8.1    2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr          1.49       2024-11-08 [1] CRAN (R 4.4.2)
#>  labeling       0.4.3      2023-08-29 [1] CRAN (R 4.4.0)
#>  lifecycle      1.0.4      2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.4.0)
#>  pillar         1.9.0      2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.4.0)
#>  plyr           1.8.9      2023-10-02 [1] CRAN (R 4.4.0)
#>  purrr          1.0.2      2023-08-10 [1] CRAN (R 4.4.0)
#>  R.cache        0.16.0     2022-07-21 [1] CRAN (R 4.4.0)
#>  R.methodsS3    1.8.2      2022-06-13 [1] CRAN (R 4.4.0)
#>  R.oo           1.26.0     2024-01-24 [1] CRAN (R 4.4.0)
#>  R.utils        2.12.3     2023-11-18 [1] CRAN (R 4.4.0)
#>  R6             2.5.1      2021-08-19 [1] CRAN (R 4.4.0)
#>  RColorBrewer   1.1-3      2022-04-03 [1] CRAN (R 4.4.0)
#>  Rcpp           1.0.13-1   2024-11-02 [1] CRAN (R 4.4.2)
#>  reprex         2.1.0      2024-01-11 [1] CRAN (R 4.4.0)
#>  reshape2       1.4.4      2020-04-09 [1] CRAN (R 4.4.0)
#>  rlang          1.1.4      2024-06-04 [1] CRAN (R 4.4.0)
#>  rmarkdown      2.29       2024-11-04 [1] CRAN (R 4.4.2)
#>  scales         1.3.0.9000 2024-11-16 [1] local
#>  sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.4.0)
#>  stringi        1.8.4      2024-05-06 [1] CRAN (R 4.4.0)
#>  stringr        1.5.1      2023-11-14 [1] CRAN (R 4.4.0)
#>  styler         1.10.3     2024-04-07 [1] CRAN (R 4.4.0)
#>  tibble         3.2.1      2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyselect     1.2.1      2024-03-11 [1] CRAN (R 4.4.0)
#>  utf8           1.2.4      2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs          0.6.5      2023-12-01 [1] CRAN (R 4.4.0)
#>  viridisLite    0.4.2      2023-05-02 [1] CRAN (R 4.4.0)
#>  withr          3.0.2      2024-10-28 [1] CRAN (R 4.4.2)
#>  xfun           0.49       2024-10-31 [1] CRAN (R 4.4.2)
#>  yaml           2.3.10     2024-07-26 [1] CRAN (R 4.4.2)
#> 
#>  [1] /home/yun/Rlibrary/4.4
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@teunbrand
Copy link
Collaborator

While I agree that it shouldn't throw an error here, I'm having a hard time to grok the intention of this code. Even in Cartesian coordinates, the axis will not display labels.

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Jan 7, 2025

Maybe something like this?

ggplot(data, aes(as.factor(Var1), Var2)) +
    geom_tile(aes(fill = value)) +
    scale_fill_viridis_c() +
    scale_x_discrete(breaks = 1:2)

Image

It seems that scale_x_discrete() behaves differently with continuous variables. I initially thought that scale_x_discrete() would match the breaks with the unique values.

@teunbrand
Copy link
Collaborator

Discrete position scales generally just keep track of the range of continuous data but don't base their categorical limits or breaks on them. The scale cannot find any discrete values, so it begets empty limits, making the breaks = 1:2 obsolete as discrete breaks are required to be a subset of discrete limits.

@teunbrand
Copy link
Collaborator

Somewhat simplified reprex, note that also using empty breaks in the radial axis causes troubles.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

p <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point() +
  coord_radial()

p + scale_x_continuous(breaks = character())
#> Error in switch(params$position, top = , right = , theta.sec = -Inf, Inf): EXPR must be a length 1 vector

p + scale_y_continuous(breaks = character())
#> Error in `setup_params()` at ggplot2/R/ggproto.R:188:17:
#> ! `params$position` must be one of "top", "right", "bottom", or "left",
#>   not "theta".

Created on 2025-01-07 with reprex v2.1.1

@teunbrand teunbrand linked a pull request Jan 7, 2025 that will close this issue
@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented Jan 8, 2025

Thanks for the PR! I’ve tested it, and it works well with this example.

Another associated warning message with this example:

Warning message:
In groups$key$position : partial match of 'position' to 'positions'

This appears to correspond to the following line in the code:

position <- groups$key$position[i]

@teunbrand
Copy link
Collaborator

Another associated warning

Thanks, nice catch!

@teunbrand teunbrand added bug an unexpected problem or unintended behavior guides 📏 coord 🗺️ labels Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior coord 🗺️ guides 📏
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants