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

layer_after_scale() breaks in dev ggplot #101

Closed
yjunechoe opened this issue Oct 8, 2024 · 0 comments · Fixed by #103
Closed

layer_after_scale() breaks in dev ggplot #101

yjunechoe opened this issue Oct 8, 2024 · 0 comments · Fixed by #103
Labels
bug Something isn't working

Comments

@yjunechoe
Copy link
Owner

Due to Layer$compute_geom_2() being recycled at an earlier step to resolve legend keys

Dev:

p <- ggplot(diamonds, aes(cut)) +
  geom_bar(aes(fill = cut))

ggtrace::layer_after_scale()
#> ✔ Executed `ggtrace_inspect_return(last_plot(), ggplot2:::Layer$compute_geom_2)`
#> # A tibble: 5 × 6
#>   fill        .id colour linewidth linetype alpha
#>   <chr>     <int> <lgl>      <dbl>    <int> <lgl>
#> 1 #440154FF     1 NA           0.5        1 NA   
#> 2 #3B528BFF     2 NA           0.5        1 NA   
#> 3 #21908CFF     3 NA           0.5        1 NA   
#> 4 #5DC863FF     4 NA           0.5        1 NA   
#> 5 #FDE725FF     5 NA           0.5        1 NA

ggtrace::inspect_n(p, ggplot2:::Layer$compute_geom_2)
#> [1] 2

CRAN:

p <- ggplot(diamonds, aes(cut)) +
  geom_bar(aes(fill = cut))

ggtrace::layer_after_scale()
#> ✔ Executed `ggtrace_inspect_return(last_plot(), ggplot2:::Layer$compute_geom_2)`
#> # A tibble: 5 × 16
#>   fill          y count  prop x     flipped_aes PANEL group  ymin  ymax xmin  xmax  colour linewidth
#>   <chr>     <dbl> <dbl> <dbl> <mpp> <lgl>       <fct> <int> <dbl> <dbl> <mpp> <mpp> <lgl>      <dbl>
#> 1 #440154FF  1610  1610     1 1     FALSE       1         1     0  1610 0.55  1.45  NA           0.5
#> 2 #3B528BFF  4906  4906     1 2     FALSE       1         2     0  4906 1.55  2.45  NA           0.5
#> 3 #21908CFF 12082 12082     1 3     FALSE       1         3     0 12082 2.55  3.45  NA           0.5
#> 4 #5DC863FF 13791 13791     1 4     FALSE       1         4     0 13791 3.55  4.45  NA           0.5
#> 5 #FDE725FF 21551 21551     1 5     FALSE       1         5     0 21551 4.55  5.45  NA           0.5
#> # ℹ 2 more variables: linetype <dbl>, alpha <lgl>

ggtrace::inspect_n(p, ggplot2:::Layer$compute_geom_2)
#> [1] 1

Temporary workaround in dev is to target the second time the method is called (layer i * 2)

p <- ggplot(diamonds, aes(cut)) +
  geom_bar(aes(fill = cut))

ggtrace::inspect_return(last_plot(), ggplot2:::Layer$compute_geom_2, cond = 2L)
#>        fill     y count prop x flipped_aes PANEL group ymin  ymax xmin xmax colour linewidth linetype alpha
#> 1 #440154FF  1610  1610    1 1       FALSE     1     1    0  1610 0.55 1.45     NA       0.5        1    NA
#> 2 #3B528BFF  4906  4906    1 2       FALSE     1     2    0  4906 1.55 2.45     NA       0.5        1    NA
#> 3 #21908CFF 12082 12082    1 3       FALSE     1     3    0 12082 2.55 3.45     NA       0.5        1    NA
#> 4 #5DC863FF 13791 13791    1 4       FALSE     1     4    0 13791 3.55 4.45     NA       0.5        1    NA
#> 5 #FDE725FF 21551 21551    1 5       FALSE     1     5    0 21551 4.55 5.45     NA       0.5        1    NA

Tracebacks:

Dev
p2 <- ggplot(diamonds, aes(cut)) +
  geom_bar(
    aes(fill = stage(
      cut, after_scale = {\(){
        print(rlang::trace_back(), srcrefs = FALSE)
        cat("\n")
      }}()
    )))
invisible(ggplot_build(p2))
#>      ▆
#>   1. ├─ggplot2::ggplot_build(p2)
#>   2. ├─ggplot2:::ggplot_build.ggplot(p2)
#>   3. │ └─plot$guides$build(npscales, plot$layers, plot$labels, data, plot$theme)
#>   4. │   └─ggplot2 (local) build(..., self = self)
#>   5. │     └─guides$process_layers(layers, layer_data, theme)
#>   6. │       └─ggplot2 (local) process_layers(..., self = self)
#>   7. │         └─base::Map(...)
#>   8. │           └─base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
#>   9. │             └─ggplot2 (local) ``(guide = dots[[1L]][[1L]], param = dots[[2L]][[1L]])
#>  10. │               └─guide$process_layers(param, layers, data, theme)
#>  11. │                 └─ggplot2 (local) process_layers(..., self = self)
#>  12. │                   └─self$get_layer_key(params, layers[include], data[include], theme)
#>  13. │                     └─ggplot2 (local) get_layer_key(...)
#>  14. │                       └─base::Map(...)
#>  15. │                         └─base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
#>  16. │                           └─ggplot2 (local) ``(layer = dots[[1L]][[1L]], df = dots[[2L]][[1L]])
#>  17. │                             └─layer$compute_geom_2(key, single_params, theme)
#>  18. │                               └─ggplot2 (local) compute_geom_2(..., self = self)
#>  19. │                                 └─self$geom$use_defaults(...)
#>  20. │                                   └─ggplot2 (local) use_defaults(..., self = self)
#>  21. │                                     └─ggplot2:::eval_aesthetics(substitute_aes(modifiers), data, mask = list(stage = stage_scaled))
#>  22. │                                       └─base::lapply(aesthetics, eval_tidy, data = data, env = env)
#>  23. │                                         └─rlang (local) FUN(X[[i]], ...)
#>  24. ├─ggplot2 (local) ``(...)
#>  25. └─{...
#> 
#>      ▆
#>   1. ├─ggplot2::ggplot_build(p2)
#>   2. ├─ggplot2:::ggplot_build.ggplot(p2)
#>   3. │ └─ggplot2:::by_layer(...)
#>   4. │   ├─rlang::try_fetch(...)
#>   5. │   │ ├─base::tryCatch(...)
#>   6. │   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   7. │   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   8. │   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   9. │   │ └─base::withCallingHandlers(...)
#>  10. │   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  11. │     └─l$compute_geom_2(d, theme = plot$theme)
#>  12. │       └─ggplot2 (local) compute_geom_2(..., self = self)
#>  13. │         └─self$geom$use_defaults(...)
#>  14. │           └─ggplot2 (local) use_defaults(..., self = self)
#>  15. │             └─ggplot2:::eval_aesthetics(substitute_aes(modifiers), data, mask = list(stage = stage_scaled))
#>  16. │               └─base::lapply(aesthetics, eval_tidy, data = data, env = env)
#>  17. │                 └─rlang (local) FUN(X[[i]], ...)
#>  18. ├─ggplot2 (local) ``(...)
#>  19. └─{...
CRAN
p2 <- ggplot(diamonds, aes(cut)) +
  geom_bar(
    aes(fill = stage(
      cut, after_scale = {\(){
        print(rlang::trace_back(), srcrefs = FALSE)
        cat("\n")
      }}()
    )))
invisible(ggplot_build(p2))
#>      ▆
#>   1. ├─ggplot2::ggplot_build(p2)
#>   2. ├─ggplot2:::ggplot_build.ggplot(p2)
#>   3. │ └─plot$guides$build(npscales, plot$layers, plot$labels, data)
#>   4. │   └─ggplot2 (local) build(..., self = self)
#>   5. │     └─guides$process_layers(layers, layer_data)
#>   6. │       └─ggplot2 (local) process_layers(..., self = self)
#>   7. │         └─base::Map(...)
#>   8. │           └─base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
#>   9. │             └─ggplot2 (local) ``(guide = dots[[1L]][[1L]], param = dots[[2L]][[1L]])
#>  10. │               └─guide$process_layers(param, layers, data)
#>  11. │                 └─ggplot2 (local) process_layers(..., self = self)
#>  12. │                   └─self$get_layer_key(params, layers[include], data[include])
#>  13. │                     └─ggplot2 (local) get_layer_key(...)
#>  14. │                       └─base::Map(...)
#>  15. │                         └─base::mapply(FUN = f, ..., SIMPLIFY = FALSE)
#>  16. │                           └─ggplot2 (local) ``(layer = dots[[1L]][[1L]], df = dots[[2L]][[1L]])
#>  17. │                             ├─rlang::try_fetch(...)
#>  18. │                             │ ├─base::tryCatch(...)
#>  19. │                             │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>  20. │                             │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>  21. │                             │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>  22. │                             │ └─base::withCallingHandlers(...)
#>  23. │                             └─layer$geom$use_defaults(...)
#>  24. │                               └─ggplot2 (local) use_defaults(..., self = self)
#>  25. │                                 └─base::lapply(substitute_aes(modifiers), eval_tidy, mask, env)
#>  26. │                                   └─rlang (local) FUN(X[[i]], ...)
#>  27. ├─ggplot2::stage(...)
#>  28. └─{...
#> 
#>      ▆
#>   1. ├─ggplot2::ggplot_build(p2)
#>   2. ├─ggplot2:::ggplot_build.ggplot(p2)
#>   3. │ └─ggplot2:::by_layer(...)
#>   4. │   ├─rlang::try_fetch(...)
#>   5. │   │ ├─base::tryCatch(...)
#>   6. │   │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#>   7. │   │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#>   8. │   │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#>   9. │   │ └─base::withCallingHandlers(...)
#>  10. │   └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
#>  11. │     └─l$compute_geom_2(d)
#>  12. │       └─ggplot2 (local) compute_geom_2(..., self = self)
#>  13. │         └─self$geom$use_defaults(data, self$aes_params, modifiers)
#>  14. │           └─ggplot2 (local) use_defaults(..., self = self)
#>  15. │             └─base::lapply(substitute_aes(modifiers), eval_tidy, mask, env)
#>  16. │               └─rlang (local) FUN(X[[i]], ...)
#>  17. ├─ggplot2::stage(...)
#>  18. └─{...
@yjunechoe yjunechoe added the bug Something isn't working label Oct 8, 2024
@yjunechoe yjunechoe linked a pull request Oct 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant