Skip to content

Commit 37205ff

Browse files
committed
Fix missing legends for unobserved levels in rhat and neff plots
closes #327
1 parent 1f66c7f commit 37205ff

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

R/mcmc-diagnostics.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ mcmc_rhat <- function(rhat, ..., size = NULL) {
142142
mapping = aes(
143143
yend = .data$parameter,
144144
xend = ifelse(min(.data$value) < 1, 1, -Inf)),
145-
na.rm = TRUE) +
145+
na.rm = TRUE,
146+
show.legend = TRUE) +
146147
bayesplot_theme_get()
147148

148149
if (min(data$value) < 1) {
@@ -238,7 +239,8 @@ mcmc_neff <- function(ratio, ..., size = NULL) {
238239
fill = .data$rating)) +
239240
geom_segment(
240241
aes(yend = .data$parameter, xend = -Inf),
241-
na.rm = TRUE) +
242+
na.rm = TRUE,
243+
show.legend = TRUE) +
242244
diagnostic_points(size) +
243245
vline_at(
244246
c(0.1, 0.5, 1),
@@ -408,7 +410,7 @@ zero_pad_int <- function(xs) {
408410
}
409411

410412
diagnostic_points <- function(size = NULL) {
411-
args <- list(shape = 21, na.rm = TRUE)
413+
args <- list(shape = 21, na.rm = TRUE, show.legend = TRUE)
412414
do.call("geom_point", c(args, size = size))
413415
}
414416

@@ -454,7 +456,6 @@ diagnostic_colors <- function(diagnostic = c("rhat", "neff_ratio"),
454456
}
455457

456458
color_labels <- diagnostic_color_labels[[diagnostic]]
457-
458459
list(diagnostic = diagnostic,
459460
aesthetic = aesthetic,
460461
color_levels = color_levels,
Lines changed: 93 additions & 0 deletions
Loading

tests/testthat/test-mcmc-diagnostics.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ test_that("mcmc_neff renders correctly", {
146146
vdiffr::expect_doppelganger("mcmc_neff (default)", p_base)
147147
})
148148

149+
test_that("mcmc_neff renders legend correctly even if some levels missing", {
150+
testthat::skip_on_cran()
151+
testthat::skip_if_not_installed("vdiffr")
152+
skip_on_r_oldrel()
153+
154+
neffs <- c(0.1, 0.2, 0.3, 0.4) # above 0.5 is missing but should still appear in legend
155+
156+
p_base <- mcmc_neff(neffs)
157+
vdiffr::expect_doppelganger("mcmc_neff (missing levels)", p_base)
158+
})
159+
149160
test_that("mcmc_neff_hist renders correctly", {
150161
testthat::skip_on_cran()
151162
testthat::skip_if_not_installed("vdiffr")

0 commit comments

Comments
 (0)