diff --git a/DESCRIPTION b/DESCRIPTION
index 677d81d1..8003c73d 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -37,6 +37,7 @@ Imports:
rlang (>= 0.3.0),
stats,
tibble (>= 2.0.0),
+ tidyr,
tidyselect,
utils
Suggests:
diff --git a/NEWS.md b/NEWS.md
index 3a211788..bbed0ed9 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -9,6 +9,7 @@
* Skip tests depending on Suggested dependency gridExtra if not installed by @MichaelChirico (#326)
* Skip tests depending on Suggested dependency rstantools if not installed by @MichaelChirico (#325)
* Update GitHub actions workflows (#338)
+* Fix missing counts in `ppc_bars_data()` by @TeemuSailynoja, thanks @famuvie (#342)
# bayesplot 1.11.1
diff --git a/R/ppc-discrete.R b/R/ppc-discrete.R
index 89f6ee00..199c394b 100644
--- a/R/ppc-discrete.R
+++ b/R/ppc-discrete.R
@@ -395,6 +395,7 @@ ppc_bars_data <-
data <-
reshape2::melt(tmp_data, id.vars = "group") %>%
count(.data$group, .data$value, .data$variable) %>%
+ tidyr::complete(.data$group, .data$value, .data$variable, fill = list(n = 0)) %>%
group_by(.data$variable, .data$group) %>%
mutate(proportion = .data$n / sum(.data$n)) %>%
ungroup() %>%
diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg
index 4884ed5f..9e825ec3 100644
--- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg
+++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-default.svg
@@ -29,17 +29,20 @@
+
-
-
+
+
+
-
+
+
diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg
index 79b7916f..d9158435 100644
--- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg
+++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-default.svg
@@ -29,17 +29,20 @@
+
-
-
+
+
+
-
-
+
+
+
@@ -54,16 +57,21 @@
+
+
+
-
-
+
+
+
-
+
+
diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg
index fc093aee..a9372d7a 100644
--- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg
+++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-grouped-facet-args-prob-size.svg
@@ -29,17 +29,20 @@
+
-
-
+
+
+
-
-
+
+
+
@@ -54,16 +57,21 @@
+
+
+
-
+
+
-
+
+
diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg
index 800dd449..3fd563df 100644
--- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg
+++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-prob-0-33-width-size-fatten.svg
@@ -29,17 +29,20 @@
+
-
-
+
+
+
-
+
+
diff --git a/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg b/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg
index 9da25a91..c8dbda46 100644
--- a/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg
+++ b/tests/testthat/_snaps/ppc-discrete/ppc-bars-width-size-fatten.svg
@@ -29,17 +29,20 @@
+
-
-
+
+
+
-
+
+
diff --git a/tests/testthat/test-ppc-discrete.R b/tests/testthat/test-ppc-discrete.R
index 2a1d5285..a5ddb622 100644
--- a/tests/testthat/test-ppc-discrete.R
+++ b/tests/testthat/test-ppc-discrete.R
@@ -68,15 +68,15 @@ test_that("ppc_bars_data includes all levels", {
# yrep has more unique values than y
d2 <- ppc_bars_data(y_ord2, yrep_ord)
expect_equal(d2$x, 1:4)
- expect_equal(d2$y_obs, c(NA, sum(tab[1:2]), tab[3:4]))
+ expect_equal(d2$y_obs, c(0, sum(tab[1:2]), tab[3:4]))
# y has more unique values than yrep
d3 <- ppc_bars_data(y_ord, yrep_ord2)
expect_equal(d3$x, 1:4)
expect_equal(d3$y_obs, tab)
- expect_equivalent(d3$l[2], NA_real_)
- expect_equivalent(d3$m[2], NA_real_)
- expect_equivalent(d3$h[2], NA_real_)
+ expect_equivalent(d3$l[2], 0)
+ expect_equivalent(d3$m[2], 0)
+ expect_equivalent(d3$h[2], 0)
})