Skip to content

Commit cfb5055

Browse files
committed
Fix violin_plot warning correctly
1 parent abcbef1 commit cfb5055

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

R/mcmc-diagnostics-nuts.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,29 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
369369
yaxis_ticks(FALSE)
370370

371371
violin_lp_data <- data.frame(treedepth, lp = lp$Value)
372+
373+
# Only keep treedepth values that occur more than once for violin plot
374+
value_counts <- table(violin_lp_data$Value)
375+
keep_values <- names(value_counts[value_counts > 1])
376+
violin_lp_data <- violin_lp_data[violin_lp_data$Value %in% keep_values, ]
377+
372378
violin_lp <-
373379
ggplot(violin_lp_data, aes(x = factor(.data$Value), y = .data$lp)) +
374380
geom_violin(fill = get_color("l"), color = get_color("lh")) +
375381
labs(x = "treedepth__", y = "lp__") +
376-
scale_x_discrete(drop = FALSE) +
377382
bayesplot_theme_get()
378383

379384
violin_accept_stat_data <- data.frame(treedepth, as = accept_stat$Value)
385+
386+
# Only keep treedepth values that occur more than once for violin plot
387+
value_counts <- table(violin_accept_stat_data$Value)
388+
keep_values <- names(value_counts[value_counts > 1])
389+
violin_accept_stat_data <- violin_accept_stat_data[violin_accept_stat_data$Value %in% keep_values, ]
390+
380391
violin_accept_stat <-
381392
ggplot(violin_accept_stat_data, aes(x = factor(.data$Value), y = .data$as)) +
382393
geom_violin(fill = get_color("l"), color = get_color("lh")) +
383394
labs(x = "treedepth__", y = "accept_stat__") +
384-
scale_x_discrete(drop = FALSE) +
385395
scale_y_continuous(breaks = c(0, 0.5, 1)) +
386396
bayesplot_theme_get()
387397

@@ -411,7 +421,6 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
411421
as_bayesplot_grid(nuts_plot)
412422
}
413423

414-
415424
#' @rdname MCMC-nuts
416425
#' @export
417426
#' @param alpha For `mcmc_nuts_energy()` only, the transparency (alpha) level

0 commit comments

Comments
 (0)