erocoar / gghalves

✂️ Easy half-half geoms in ggplot2
https://erocoar.github.io/gghalves/
Other
241 stars 13 forks source link

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

Open tungttnguyen opened 1 year ago

tungttnguyen commented 1 year ago

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

Pengzhi-Gao commented 4 months ago

Also, I found this question in geom_half_violin(). This is my code and the error information.

>     p <- ggplot(count, aes(x = Type1, y = value, fill = State)) +
+       # 分面小提琴图
+       geom_half_violin(
+         data = count %>% filter(Sample == "Normal"),
+         aes(x = Type1, y = value), trim = FALSE,
+         fill = "#A9CCE3", colour = NA, side = "l", scale = 'width'
+       ) +
+       geom_half_violin(
+         data = count %>% filter(Sample == sample_label),
+         aes(x = Type1, y = value), trim = FALSE,
+         fill = "#dfb424", colour = NA, side = "r", scale = 'width'
+       ) +
+       scale_y_continuous(limits = c(0, NA)) +
+       scale_fill_manual(name = 'Sample_type',
+                         breaks = c('Normal', sample_label),
+                         values = c('Normal' = '#A9CCE3',sample_label = '#dfb424')) +
+       theme_bw() +
+       # 修改文本
+       theme(plot.title = element_text(size = 17, hjust = 0.5),
+             panel.background = element_blank(),
+             panel.grid.major = element_blank(),
+             panel.grid.minor = element_blank(),
+             panel.border = element_rect(size = 1),
+             axis.text.x = element_text(size = 15, angle = 45, hjust = 1, vjust = 1),  # 旋转x轴文本
+             axis.text.y = element_text(size = 15),
+             axis.title = element_text(size = 15),
+             plot.margin = margin(t = 0.4, b = 0, r = 1, l = 1, unit = "cm")) +
+       labs(x = '',
+            y = 'log2(TPM)',
+            title = paste(sample_label, gene_name, collapse = " "))
Warning message:
The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. 
>     p
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
Run `rlang::last_trace()` to see where the error occurred.
Warning messages:
1: Groups with fewer than two datapoints have been dropped.
ℹ Set `drop = FALSE` to consider such groups for position adjustment purposes. 
2: No shared levels found between `names(values)` of the manual scale and the data's fill values. 
3: Removed 934 rows containing missing values or values outside the scale range (`geom_half_violin()`). 
4: Removed 1350 rows containing missing values or values outside the scale range (`geom_half_violin()`). 
Connected to your session in progress, last started 2024-Apr-27 12:10:01 UTC (14 minutes ago)

Could you provide a solution to this question? Please let me know.