diff --git a/NEWS.md b/NEWS.md
index 4138aba6..c306f31b 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,5 +1,6 @@
# bayesplot (development version)
+* Fixed bug in `mcmc_pairs()` (introduced in bayesplot 1.10.0) where some panels' divergences/treedepth hits were missing(#555)
* Unified density-control argument defaults (`bw`, `adjust`, `kernel`, `n_dens`) to use `NULL` with internal fallbacks. No change in user-facing behavior.
* `prepare_mcmc_array()` now warns instead of erroring on `NA`s in the input.
* Fixed `validate_chain_list()` colnames check to compare all chains, not just the first two.
diff --git a/R/mcmc-scatterplots.R b/R/mcmc-scatterplots.R
index 6d7d0057..08edcfd6 100644
--- a/R/mcmc-scatterplots.R
+++ b/R/mcmc-scatterplots.R
@@ -405,9 +405,14 @@ mcmc_pairs <- function(x,
divs_j_fac <- factor(as.logical(divs_j),
levels = c(FALSE, TRUE),
labels = c("NoDiv", "Div"))
+ # pass the indicator via `data` so each panel keeps its own values
+ # (a bare loop variable in aes() is only evaluated at plot-build time,
+ # when it would have just the value from the last panel)
+ # https://github.com/stan-dev/bayesplot/issues/555
plots[[j]] <- plots[[j]] +
geom_point(
- aes(color = divs_j_fac, size = divs_j_fac),
+ aes(color = .data$Divergent, size = .data$Divergent),
+ data = data.frame(x = x_j[, 1], y = x_j[, 2], Divergent = divs_j_fac),
shape = np_style$shape[["div"]],
alpha = np_style$alpha[["div"]],
na.rm = TRUE
@@ -418,7 +423,8 @@ mcmc_pairs <- function(x,
labels = c("NoHit", "Hit"))
plots[[j]] <- plots[[j]] +
geom_point(
- aes(color = max_td_hit_j_fac, size = max_td_hit_j_fac),
+ aes(color = .data$MaxTreedepth, size = .data$MaxTreedepth),
+ data = data.frame(x = x_j[, 1], y = x_j[, 2], MaxTreedepth = max_td_hit_j_fac),
shape = np_style$shape[["td"]],
alpha = np_style$alpha[["td"]],
na.rm = TRUE
diff --git a/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-hex.svg b/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-hex.svg
index 7fc9a7e1..03a41409 100644
--- a/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-hex.svg
+++ b/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-hex.svg
@@ -455,52 +455,48 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-td.svg b/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-td.svg
index 2b227f37..d91b0a73 100644
--- a/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-td.svg
+++ b/tests/testthat/_snaps/mcmc-scatter-and-parcoord/mcmc-pairs-divs-td.svg
@@ -1093,354 +1093,412 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+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+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1403,34 +1465,26 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/tests/testthat/test-mcmc-scatter-and-parcoord.R b/tests/testthat/test-mcmc-scatter-and-parcoord.R
index 1cc70889..4c7d3de1 100644
--- a/tests/testthat/test-mcmc-scatter-and-parcoord.R
+++ b/tests/testthat/test-mcmc-scatter-and-parcoord.R
@@ -131,6 +131,58 @@ test_that("mcmc_pairs works with NUTS info", {
})
+test_that("mcmc_pairs panels show their own divergences and treedepth hits", {
+ #https://github.com/stan-dev/bayesplot/issues/555
+ skip_if_not_installed("gridExtra")
+
+ set.seed(42)
+ n_iter <- 100
+ n_chain <- 4
+ x2 <- array(rnorm(n_iter * n_chain * 2), dim = c(n_iter, n_chain, 2),
+ dimnames = list(NULL, NULL, c("alpha", "beta")))
+
+ # divergences in chains 2 (25) and 4 (4), max treedepth hits in chain 1 (10),
+ # each at distinctive locations so we can check they're plotted correctly
+ x2[1:25, 2, ] <- 50
+ x2[1:4, 4, ] <- -50
+ x2[1:10, 1, ] <- 80
+
+ divergent <- treedepth <- matrix(0, nrow = n_iter, ncol = n_chain)
+ divergent[1:25, 2] <- 1
+ divergent[1:4, 4] <- 1
+ treedepth[1:10, 1] <- 10
+ np2 <- data.frame(
+ Iteration = rep(seq_len(n_iter), times = 2 * n_chain),
+ Parameter = rep(c("divergent__", "treedepth__"), each = n_iter * n_chain),
+ Value = c(divergent, treedepth),
+ Chain = rep(rep(seq_len(n_chain), each = n_iter), times = 2)
+ )
+
+ p <- mcmc_pairs(x2, np = np2, max_treedepth = 9,
+ condition = pairs_condition(chains = list(1:2, 3:4)))
+
+ nuts_points <- function(gg, color) {
+ b <- ggplot2::ggplot_build(gg)
+ d <- do.call(rbind, lapply(b$data, function(l) l[, c("x", "colour")]))
+ d[!is.na(d$colour) & d$colour == color, ]
+ }
+
+ # panel 2 is above the diagonal (chains 1:2), panel 3 below (chains 3:4)
+ divs_upper <- nuts_points(p$bayesplots[[2]], "red")
+ divs_lower <- nuts_points(p$bayesplots[[3]], "red")
+ expect_equal(nrow(divs_upper), 25)
+ expect_equal(nrow(divs_lower), 4)
+ expect_true(all(divs_upper$x == 50))
+ expect_true(all(divs_lower$x == -50))
+
+ td_upper <- nuts_points(p$bayesplots[[2]], "yellow2")
+ td_lower <- nuts_points(p$bayesplots[[3]], "yellow2")
+ expect_equal(nrow(td_upper), 10)
+ expect_equal(nrow(td_lower), 0)
+ expect_true(all(td_upper$x == 80))
+})
+
+
test_that("mcmc_pairs throws correct warnings and errors", {
skip_if_not_installed("rstanarm")