From 9f9dbff95988a43b7ba3b6934c9cfd2e73f4dab9 Mon Sep 17 00:00:00 2001 From: Indrajeet Patil Date: Fri, 17 May 2024 02:48:51 +0200 Subject: [PATCH] Don't drop empty groups in `ggpiestats()` closes #935 --- NEWS.md | 4 + R/ggbarstats.R | 2 +- R/ggpiestats.R | 2 +- .../ggcoefstats/meta-analysis-works.svg | 147 ++++++++++++++++++ .../empty-groups-in-factors-not-dropped.svg | 105 +++++++++++++ tests/testthat/test-ggpiestats.R | 10 ++ 6 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/ggcoefstats/meta-analysis-works.svg create mode 100644 tests/testthat/_snaps/ggpiestats/empty-groups-in-factors-not-dropped.svg diff --git a/NEWS.md b/NEWS.md index 9206c284b..a35666a19 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,10 @@ N.B. All statistical analysis in `{ggstatsplot}` is carried out in read the `NEWS` for that package: +## BUG FIXES + +- Empty groups in factors are no longer dropped in `ggpiestats()` and `ggbarstats()` (#935). + # ggstatsplot 0.12.3 ## MAJOR CHANGES diff --git a/R/ggbarstats.R b/R/ggbarstats.R index 85167c293..aa94066dc 100644 --- a/R/ggbarstats.R +++ b/R/ggbarstats.R @@ -83,7 +83,7 @@ ggbarstats <- function( if (".counts" %in% names(data)) data %<>% tidyr::uncount(weights = .counts) # x and y need to be a factor; also drop the unused levels of the factors - data %<>% mutate(across(.cols = everything(), .fns = ~ droplevels(as.factor(.x)))) + data %<>% mutate(across(.cols = everything(), .fns = ~ as.factor(.x))) # TO DO: until one-way table is supported by `BayesFactor` if (nlevels(pull(data, {{ y }})) == 1L) c(bf.message, proportion.test) %<-% c(FALSE, FALSE) diff --git a/R/ggpiestats.R b/R/ggpiestats.R index c3d8cc239..6685f0247 100644 --- a/R/ggpiestats.R +++ b/R/ggpiestats.R @@ -110,7 +110,7 @@ ggpiestats <- function( if (".counts" %in% names(data)) data %<>% tidyr::uncount(weights = .counts) # x and y need to be a factor; also drop the unused levels of the factors - data %<>% mutate(across(.cols = everything(), .fns = ~ droplevels(as.factor(.x)))) + data %<>% mutate(across(.cols = everything(), .fns = ~ as.factor(.x))) x_levels <- nlevels(pull(data, {{ x }})) y_levels <- ifelse(test == "one.way", 0L, nlevels(pull(data, {{ y }}))) diff --git a/tests/testthat/_snaps/ggcoefstats/meta-analysis-works.svg b/tests/testthat/_snaps/ggcoefstats/meta-analysis-works.svg new file mode 100644 index 000000000..6af7524f7 --- /dev/null +++ b/tests/testthat/_snaps/ggcoefstats/meta-analysis-works.svg @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +term_1 +term_2 +term_3 +term_4 +term_5 + + + + + + + + + +0.0 +0.3 +0.6 +0.9 +estimate +term +z += +2.17 +, + +p += +0.03 +, + +β + +summary +meta += +0.44 +, + +C +I +95% + +[ +0.04 +, + +0.83 +] +, + +n +effects += +5 +l +o +g +e +( +B +F +01 +) += +-0.44 +, + +δ + +difference +posterior += +0.39 +, + +C +I +95% +E +T +I + +[ +-0.06 +, + +0.79 +] +, + +r +Cauchy +JZS += +0.71 + + diff --git a/tests/testthat/_snaps/ggpiestats/empty-groups-in-factors-not-dropped.svg b/tests/testthat/_snaps/ggpiestats/empty-groups-in-factors-not-dropped.svg new file mode 100644 index 000000000..a2bb65501 --- /dev/null +++ b/tests/testthat/_snaps/ggpiestats/empty-groups-in-factors-not-dropped.svg @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +100% + + + +smoker + + +no +χ +gof +2 +( +1 +) += +5.00 +, + +p += +0.03 +, + +C + +Pearson += +0.71 +, + +C +I +95% + +[ +0.00 +, + +0.88 +] +, + +n +obs += +5 +l +o +g +e +( +B +F +01 +) += +-2.06 +, + +a +Gunel-Dickey += +1.00 + + diff --git a/tests/testthat/test-ggpiestats.R b/tests/testthat/test-ggpiestats.R index cbd908a3b..008bd30f4 100644 --- a/tests/testthat/test-ggpiestats.R +++ b/tests/testthat/test-ggpiestats.R @@ -258,5 +258,15 @@ test_that( proportion.test = FALSE ) ) + + smokers <- tibble( + smoker = factor(c("no", "no", "no", "no", "no"), levels = c("yes", "no")) + ) + + set.seed(123) + expect_doppelganger( + title = "empty groups in factors not dropped", + fig = ggpiestats(smokers, smoker) + ) } )