Skip to content

Error in geom_half_violin: Error occurred in the 2nd layer #23

@tungttnguyen

Description

@tungttnguyen

Hi Frederik,

I ran into the following error when using geom_half_violin(). Could you take a look to see what went wrong? Thanks!

Error in `geom_half_violin()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 2nd layer.
Caused by error in `if ((is_panel & (side[1] == "l")) | is_group) ...`:
! missing value where TRUE/FALSE needed
library(tidyr)
library(gghalves)
#> Loading required package: ggplot2
packageVersion("gghalves")
#> [1] '0.1.4'

dat_wide <- data.frame(
  Date = c("1971-04-15","1972-04-15","1973-04-15",
           "1974-04-15","1975-04-15","1976-04-15",
           "1977-04-15","1978-04-15","1979-04-15",
           "1980-04-15","1981-04-15","1982-04-15",
           "1983-04-15","1984-04-15","1985-04-15",
           "1986-04-15","1987-04-15","1988-04-15",
           "1989-04-15","1971-05-15","1972-05-15",
           "1973-05-15","1974-05-15","1975-05-15",
           "1976-05-15","1977-05-15","1978-05-15","1979-05-15",
           "1980-05-15","1981-05-15","1982-05-15",
           "1983-05-15","1984-05-15","1985-05-15",
           "1986-05-15","1987-05-15","1988-05-15",
           "1989-05-15","1971-06-15","1972-06-15",
           "1973-06-15","1974-06-15","1975-06-15",
           "1976-06-15","1977-06-15","1978-06-15",
           "1979-06-15","1980-06-15","1981-06-15",
           "1982-06-15","1983-06-15","1984-06-15",
           "1985-06-15","1986-06-15","1987-06-15",
           "1988-06-15","1989-06-15"),
  Layer0 = c(0,
             0,0,847,0,0,0,0,0,0,0,65,42,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  Layer1 = c(0,
             0,0,13330,0,0,0,0,0,0,0,14220,9,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
  Layer2 = c(0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0),
  Layer3 = c(0,
             0,0,1450,0,0,0,0,0,0,0,1617,27,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0),
  MonthN = as.factor(c("Apr","Apr",
                       "Apr","Apr","Apr","Apr","Apr",
                       "Apr","Apr","Apr","Apr","Apr",
                       "Apr","Apr","Apr","Apr",
                       "Apr","Apr","Apr","May","May",
                       "May","May","May","May","May",
                       "May","May","May","May",
                       "May","May","May","May","May",
                       "May","May","May","Jun","Jun",
                       "Jun","Jun","Jun","Jun","Jun",
                       "Jun","Jun","Jun","Jun",
                       "Jun","Jun","Jun","Jun","Jun",
                       "Jun","Jun","Jun"))
)

dat <- dat_wide %>% 
  pivot_longer(-c(Date, MonthN),
               names_to = "Layer",
               values_to = "Value")
dat
#> # A tibble: 228 × 4
#>    Date       MonthN Layer  Value
#>    <chr>      <fct>  <chr>  <dbl>
#>  1 1971-04-15 Apr    Layer0     0
#>  2 1971-04-15 Apr    Layer1     0
#>  3 1971-04-15 Apr    Layer2     0
#>  4 1971-04-15 Apr    Layer3     0
#>  5 1972-04-15 Apr    Layer0     0
#>  6 1972-04-15 Apr    Layer1     0
#>  7 1972-04-15 Apr    Layer2     0
#>  8 1972-04-15 Apr    Layer3     0
#>  9 1973-04-15 Apr    Layer0     0
#> 10 1973-04-15 Apr    Layer1     0
#> # ℹ 218 more rows

p1 <- ggplot(dat,
             aes(x = Layer,
                 y = Value,
                 color = Layer)) +
  geom_half_boxplot(nudge = 0.035,
                    errorbar.draw = TRUE) +
  geom_half_violin(aes(fill = Layer),
                   side = "r", nudge = 0.005) +
  scale_y_log10(labels = scales::comma) +
  annotation_logticks(sides = 'lr') +
  theme_bw(base_size = 16) +
  theme(legend.position = 'none') 
p1
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Transformation introduced infinite values in continuous y-axis
#> Warning: Removed 219 rows containing non-finite values (`stat_boxplot()`).
#> Warning: Removed 219 rows containing non-finite values
#> (`stat_half_ydensity()`).
#> Error in `geom_half_violin()`:
#> ! Problem while converting geom to grob.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error in `if ((is_panel & (side[1] == "l")) | is_group) ...`:
#> ! missing value where TRUE/FALSE needed
#> 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("fishy-coral_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(..., self = self)
#>  62. │                                                     └─base::lapply(...)
#>  63. │                                                       └─ggplot2 (local) FUN(X[[i]], ...)
#>  64. │                                                         └─self$draw_group(group, panel_params, coord, ...)
#>  65. │                                                           └─gghalves (local) draw_group(..., self = self)
#>  66. └─base::.handleSimpleError(...)
#>  67.   └─rlang (local) h(simpleError(msg, call))
#>  68.     └─handlers[[1L]](cnd)
#>  69.       └─cli::cli_abort(...)
#>  70.         └─rlang::abort(...)

It worked with either geom_boxplot() or geom_violin()

p2 <- ggplot(dat,
             aes(x = Layer,
                 y = Value,
                 color = Layer)) +
  geom_boxplot() +
  theme_bw(base_size = 16) +
  theme(legend.position = 'none') +
  scale_y_log10(labels = scales::comma) +
  annotation_logticks(sides = 'lr')
p2
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Warning: Removed 219 rows containing non-finite values (`stat_boxplot()`).

p3 <- ggplot(dat,
             aes(x = Layer,
                 y = Value,
                 color = Layer)) +
  geom_violin() +
  theme_bw(base_size = 16) +
  theme(legend.position = 'none') +
  scale_y_log10(labels = scales::comma) +
  annotation_logticks(sides = 'lr') 
p3
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Warning: Removed 219 rows containing non-finite values (`stat_ydensity()`).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions