Skip to content

Error in annotation_logticks Problem while converting geom to grob caused by error in unit() #5248

Closed

Description

@tungttnguyen

This is a follow up to the previous issue #5229.
Here is the error message:

Error in `annotation_logticks()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 2nd layer.
Caused by error in `unit()`:
! 'x' and 'units' must have length > 0

Code to reproduce the bug:

library(scales)
library(ggplot2)
packageVersion("ggplot2")
#> [1] '3.4.1.9000'

dat <- data.frame(
  Percentage = c(0.00342281879194,0.0097335470299509,1,1,1.00,1.00000),
  Value = c(965.002039355444, 922.752039355444,0,0,0.0,0.000),
  ID = as.factor(c("A","B","B","A", "B", "A"))
)

p1 <- ggplot(dat,
             aes(Percentage, Value,
                 color = ID)) +
  geom_line(linewidth = 0.75) +
  theme_bw(base_size = 16) +
  scale_x_continuous(expand = c(0, 0),
                     breaks = c(0.01, 0.25, 0.5, 0.75),
                     labels = scales::percent_format(accuracy = 1L)) +
  scale_y_log10(labels = scales::comma) 
p1 +
  annotation_logticks(sides = 'lr')
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Error in `annotation_logticks()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error in `unit()`:
#> ! 'x' and 'units' must have length > 0
#> 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("wax-mouse_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_gtable(data)
#>  44. │                               └─ggplot2:::ggplot_gtable.ggplot_built(data)
#>  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$draw_geom(d, layout)
#>  54. │                                       └─ggplot2 (local) draw_geom(..., self = self)
#>  55. │                                         └─self$geom$draw_layer(...)
#>  56. │                                           └─ggplot2 (local) draw_layer(..., self = self)
#>  57. │                                             └─base::lapply(...)
#>  58. │                                               └─ggplot2 (local) FUN(X[[i]], ...)
#>  59. │                                                 ├─rlang::inject(self$draw_panel(data, panel_params, coord, !!!params))
#>  60. │                                                 └─self$draw_panel(...)
#>  61. │                                                   └─ggplot2 (local) draw_panel(...)
#>  62. │                                                     ├─base::with(...)
#>  63. │                                                     └─base::with.default(...)
#>  64. │                                                       └─base::eval(substitute(expr), data, enclos = parent.frame())
#>  65. │                                                         └─base::eval(substitute(expr), data, enclos = parent.frame())
#>  66. │                                                           ├─grid::segmentsGrob(...)
#>  67. │                                                           │ └─grid::is.unit(x0)
#>  68. │                                                           └─grid::unit(yticks$start, "cm")
#>  69. │                                                             └─base::stop("'x' and 'units' must have length > 0")
#>  70. └─base::.handleSimpleError(...)
#>  71.   └─rlang (local) h(simpleError(msg, call))
#>  72.     └─handlers[[1L]](cnd)
#>  73.       └─cli::cli_abort(...)
#>  74.         └─rlang::abort(...)

Created on 2023-03-24 with reprex v2.0.2

Activity

  1. added
    bugan unexpected problem or unintended behavior
    on Mar 27, 2023
  2. teunbrand commented on Mar 27, 2023

    @teunbrand
    Collaborator

    Thanks for the report! I can confirm this bug.

  3. tungttnguyen commented on Mar 27, 2023

    @tungttnguyen
    Author

    Thanks for checking @teunbrand !

  4. teunbrand commented on Apr 3, 2023

    @teunbrand
    Collaborator

    Here is a more minimal reprex (backtrace omitted):

    library(ggplot2)
    
    ggplot(mpg, aes(displ, year - 20)) +
      geom_point() +
      scale_y_log10() +
      annotation_logticks(sides = 'l')
    #> Error in `annotation_logticks()`:
    #> ! Problem while converting geom to grob.
    #> ℹ Error occurred in the 2nd layer.
    #> Caused by error in `unit()`:
    #> ! 'x' and 'units' must have length > 0

    Created on 2023-04-03 with reprex v2.0.2

    I think this problem happens when the data range is such that zero appropriate ticks are found. If we omit the - 20 above, it finds '2000' as an appropriate tick and the problem doesn't occur.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions