From 89cfcdd5e9c52736d34263316415a704e96ab7b0 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Sun, 15 Mar 2026 20:21:49 +0530 Subject: [PATCH 1/3] Replace c(mult, add) with expansion() in scale expand args --- NEWS.md | 1 + R/helpers-gg.R | 4 ++-- R/mcmc-diagnostics-nuts.R | 2 +- R/mcmc-diagnostics.R | 8 ++++---- R/mcmc-distributions.R | 2 +- R/mcmc-parcoord.R | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index bf6aaef8..9610a131 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,7 @@ * New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402) * Default to `quantiles=100` for all dot plots by @behramulukir (#402) * Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`. +* Replace old-style `expand = c(mult, add)` with `expansion()` helper in scale functions across `helpers-gg.R`, `mcmc-diagnostics.R`, `mcmc-diagnostics-nuts.R`, `mcmc-distributions.R`, and `mcmc-parcoord.R` for consistency with ggplot2 >= 3.3.0 style. # bayesplot 1.15.0 diff --git a/R/helpers-gg.R b/R/helpers-gg.R index 21ec794a..989db1d7 100644 --- a/R/helpers-gg.R +++ b/R/helpers-gg.R @@ -37,10 +37,10 @@ modify_aes <- function(mapping, ...) { facet_wrap_parsed <- function(...) { facet_wrap(..., labeller = label_parsed) } -dont_expand_y_axis <- function(expand = c(0,0)) { +dont_expand_y_axis <- function(expand = expansion(0, 0)) { scale_y_continuous(expand = expand) } -dont_expand_x_axis <- function(expand = c(0,0)) { +dont_expand_x_axis <- function(expand = expansion(0, 0)) { scale_x_continuous(expand = expand) } dont_expand_axes <- function() { diff --git a/R/mcmc-diagnostics-nuts.R b/R/mcmc-diagnostics-nuts.R index fa651efb..47289ba4 100644 --- a/R/mcmc-diagnostics-nuts.R +++ b/R/mcmc-diagnostics-nuts.R @@ -484,7 +484,7 @@ mcmc_nuts_energy <- scale_fill_manual("", values = fills, labels = aes_labs) + scale_color_manual("", values = clrs, labels = aes_labs) + dont_expand_y_axis(c(0.005, 0)) + - scale_x_continuous(expand = c(0.2, 0)) + + scale_x_continuous(expand = expansion(mult = 0.2, add = 0)) + labs(y = NULL, x = expression(E - bar(E))) + bayesplot_theme_get() + space_legend_keys() + diff --git a/R/mcmc-diagnostics.R b/R/mcmc-diagnostics.R index 2a71b8e0..59e5ea33 100644 --- a/R/mcmc-diagnostics.R +++ b/R/mcmc-diagnostics.R @@ -170,8 +170,8 @@ mcmc_rhat <- function(rhat, ..., size = NULL) { labs(y = NULL, x = expression(hat(R))) + scale_fill_diagnostic("rhat") + scale_color_diagnostic("rhat") + - scale_x_continuous(breaks = brks, expand = c(0, .01)) + - scale_y_discrete(expand = c(.025,0)) + + scale_x_continuous(breaks = brks, expand = expansion(mult = 0, add = 0.01)) + + scale_y_discrete(expand = expansion(mult = 0.025, add = 0)) + yaxis_title(FALSE) + yaxis_text(FALSE) + yaxis_ticks(FALSE) @@ -262,7 +262,7 @@ mcmc_neff <- function(ratio, ..., size = NULL) { # as.character truncates trailing zeroes, while ggplot default does not labels = as.character(breaks), limits = c(0, max(1, max_ratio) + 0.05), - expand = c(0, 0)) + + expand = expansion(0, 0)) + bayesplot_theme_get() + yaxis_text(FALSE) + yaxis_title(FALSE) + @@ -574,7 +574,7 @@ drop_NAs_and_warn <- function(x) { scale_x_continuous( limits = c(-0.5, lags + 0.5), breaks = function(x) as.integer(pretty(x, n = 3)), - expand = c(0, 0) + expand = expansion(0, 0) ) + labs(x = "Lag", y = "Autocorrelation") + force_axes_in_facets() diff --git a/R/mcmc-distributions.R b/R/mcmc-distributions.R index 54f90ff8..1061e033 100644 --- a/R/mcmc-distributions.R +++ b/R/mcmc-distributions.R @@ -332,7 +332,7 @@ mcmc_dens_chains <- function( labs(color = "Chain") + scale_y_discrete( limits = unique(rev(data$parameter)), - expand = c(0.05, .6) + expand = expansion(mult = 0.05, add = 0.6) ) + scale_color + bayesplot_theme_get() + diff --git a/R/mcmc-parcoord.R b/R/mcmc-parcoord.R index d5590d2b..a0cc54ca 100644 --- a/R/mcmc-parcoord.R +++ b/R/mcmc-parcoord.R @@ -159,7 +159,7 @@ mcmc_parcoord <- } graph + - scale_x_discrete(expand = c(0,0), labels = levels(draws$Parameter)) + + scale_x_discrete(expand = expansion(0, 0), labels = levels(draws$Parameter)) + expand_limits(x = nlevels(draws$Parameter) + 0.25) + labs(x = NULL, y = NULL) } From 0d52cf97a0931c7ab7027d094dc478dcb76ef038 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Mon, 16 Mar 2026 23:30:43 +0530 Subject: [PATCH 2/3] Update dont_expand_y_axis() call sites to use expansion() --- R/mcmc-diagnostics-nuts.R | 4 ++-- R/mcmc-diagnostics.R | 4 ++-- R/mcmc-distributions.R | 6 +++--- R/mcmc-traces.R | 2 +- R/ppc-test-statistics.R | 2 +- R/ppd-test-statistics.R | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/R/mcmc-diagnostics-nuts.R b/R/mcmc-diagnostics-nuts.R index 47289ba4..a7a7f3eb 100644 --- a/R/mcmc-diagnostics-nuts.R +++ b/R/mcmc-diagnostics-nuts.R @@ -183,7 +183,7 @@ mcmc_nuts_acceptance <- ) } hists <- hists + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + facet_wrap(vars(.data$Parameter), scales = "free") + yaxis_text(FALSE) + yaxis_title(FALSE) + @@ -483,7 +483,7 @@ mcmc_nuts_energy <- ) + scale_fill_manual("", values = fills, labels = aes_labs) + scale_color_manual("", values = clrs, labels = aes_labs) + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + scale_x_continuous(expand = expansion(mult = 0.2, add = 0)) + labs(y = NULL, x = expression(E - bar(E))) + bayesplot_theme_get() + diff --git a/R/mcmc-diagnostics.R b/R/mcmc-diagnostics.R index 59e5ea33..deff783c 100644 --- a/R/mcmc-diagnostics.R +++ b/R/mcmc-diagnostics.R @@ -199,7 +199,7 @@ mcmc_rhat_hist <- function(rhat, ..., binwidth = NULL, bins = NULL, breaks = NUL scale_color_diagnostic("rhat") + scale_fill_diagnostic("rhat") + labs(x = expression(hat(R)), y = NULL) + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + yaxis_title(FALSE) + yaxis_text(FALSE) + @@ -290,7 +290,7 @@ mcmc_neff_hist <- function(ratio, ..., binwidth = NULL, bins = NULL, breaks = NU scale_color_diagnostic("neff_ratio") + scale_fill_diagnostic("neff_ratio") + labs(x = expression(N[eff]/N), y = NULL) + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + yaxis_title(FALSE) + yaxis_text(FALSE) + yaxis_ticks(FALSE) + diff --git a/R/mcmc-distributions.R b/R/mcmc-distributions.R index 1061e033..dd4adb38 100644 --- a/R/mcmc-distributions.R +++ b/R/mcmc-distributions.R @@ -523,7 +523,7 @@ mcmc_dots_by_chain <- function( } graph + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + yaxis_text(FALSE) + yaxis_title(FALSE) + @@ -642,7 +642,7 @@ mcmc_dots_by_chain <- function( } graph + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + yaxis_text(FALSE) + yaxis_ticks(FALSE) + @@ -703,7 +703,7 @@ mcmc_dots_by_chain <- function( } graph + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + yaxis_text(FALSE) + yaxis_title(FALSE) + diff --git a/R/mcmc-traces.R b/R/mcmc-traces.R index a6f2387e..78990ceb 100644 --- a/R/mcmc-traces.R +++ b/R/mcmc-traces.R @@ -446,7 +446,7 @@ mcmc_rank_hist <- function(x, geom_blank(data = data_boundaries) + facet_call + force_x_axis_in_facets() + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + theme( axis.line.y = element_blank(), diff --git a/R/ppc-test-statistics.R b/R/ppc-test-statistics.R index 73b74769..99df3953 100644 --- a/R/ppc-test-statistics.R +++ b/R/ppc-test-statistics.R @@ -263,7 +263,7 @@ ppc_stat_freqpoly <- values = set_names(get_color(c("m", "dh")), c("yrep", "y")), labels = c(yrep = Tyrep_label(), y = Ty_label()) ) + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + xaxis_title(FALSE) + yaxis_text(FALSE) + diff --git a/R/ppd-test-statistics.R b/R/ppd-test-statistics.R index d7146405..f1c9d07d 100644 --- a/R/ppd-test-statistics.R +++ b/R/ppd-test-statistics.R @@ -143,7 +143,7 @@ ppd_stat_freqpoly <- name = stat_legend_title(stat, deparse(substitute(stat))), labels = Typred_label() ) + - dont_expand_y_axis(c(0.005, 0)) + + dont_expand_y_axis(expansion(mult = 0.005, add = 0)) + bayesplot_theme_get() + xaxis_title(FALSE) + yaxis_text(FALSE) + From 36702effe67afcbe8d4ea56068e7aabf460432ec Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 16 Mar 2026 15:16:47 -0600 Subject: [PATCH 3/3] Add a few small regression tests for the panel expansion --- NEWS.md | 2 +- tests/testthat/test-mcmc-diagnostics.R | 8 ++++++++ tests/testthat/test-mcmc-distributions.R | 13 ++++++++++++- tests/testthat/test-mcmc-scatter-and-parcoord.R | 8 ++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 9610a131..9418e399 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,7 +11,7 @@ * New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402) * Default to `quantiles=100` for all dot plots by @behramulukir (#402) * Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`. -* Replace old-style `expand = c(mult, add)` with `expansion()` helper in scale functions across `helpers-gg.R`, `mcmc-diagnostics.R`, `mcmc-diagnostics-nuts.R`, `mcmc-distributions.R`, and `mcmc-parcoord.R` for consistency with ggplot2 >= 3.3.0 style. +* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style. # bayesplot 1.15.0 diff --git a/tests/testthat/test-mcmc-diagnostics.R b/tests/testthat/test-mcmc-diagnostics.R index 5e95f46b..9c6efb74 100644 --- a/tests/testthat/test-mcmc-diagnostics.R +++ b/tests/testthat/test-mcmc-diagnostics.R @@ -79,6 +79,14 @@ test_that("'description' & 'rating' columns are correct (#176)", { expect_equal(df$description, expected_descriptions) }) +test_that("mcmc_rhat uses the expected panel expansion", { + rhats <- seq(from = 1, to = 1.20, length.out = 10) + built <- ggplot2::ggplot_build(mcmc_rhat(rhats)) + + expect_equal(built$layout$panel_params[[1]]$x.range, c(0.99, 1.21)) + expect_equal(built$layout$panel_params[[1]]$y.range, c(0.775, 10.225)) +}) + test_that("mcmc_acf & mcmc_acf_bar return a ggplot object", { expect_gg(mcmc_acf(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]")) expect_gg(mcmc_acf_bar(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]")) diff --git a/tests/testthat/test-mcmc-distributions.R b/tests/testthat/test-mcmc-distributions.R index 959cc315..8b280f30 100644 --- a/tests/testthat/test-mcmc-distributions.R +++ b/tests/testthat/test-mcmc-distributions.R @@ -171,6 +171,18 @@ test_that("mcmc_dots_by_chain returns a ggplot object", { quantiles = 80)) }) +test_that("mcmc_dots_by_chain uses the expected y-axis expansion", { + built <- ggplot2::ggplot_build( + mcmc_dots_by_chain( + example_mcmc_draws(), + pars = c("beta[1]", "beta[2]"), + quantiles = 50 + ) + ) + + expect_equal(built$layout$panel_params[[1]]$y.range, c(-0.005, 1.005)) +}) + test_that("mcmc_violin returns a ggplot object", { expect_gg(mcmc_violin(arr, pars = "beta[2]", regex_pars = "x\\:")) expect_gg(mcmc_violin(dframe_multiple_chains, @@ -294,4 +306,3 @@ test_that("mcmc_dots_by_chain renders correctly", { p_base <- mcmc_dots_by_chain(vdiff_dframe_chains) vdiffr::expect_doppelganger("mcmc_dots_by_chain (default)", p_base) }) - diff --git a/tests/testthat/test-mcmc-scatter-and-parcoord.R b/tests/testthat/test-mcmc-scatter-and-parcoord.R index 570e06e8..ddae73cc 100644 --- a/tests/testthat/test-mcmc-scatter-and-parcoord.R +++ b/tests/testthat/test-mcmc-scatter-and-parcoord.R @@ -320,6 +320,14 @@ test_that("mcmc_parcoord returns a ggplot object", { expect_gg(mcmc_parcoord(arr, pars = "sigma", regex_pars = "beta")) }) +test_that("mcmc_parcoord uses the expected x-axis expansion", { + built <- ggplot2::ggplot_build( + mcmc_parcoord(example_mcmc_draws(), pars = c("beta[1]", "beta[2]", "sigma")) + ) + + expect_equal(built$layout$panel_params[[1]]$x.range, c(1, 3.25)) +}) + test_that("mcmc_parcoord with nuts info returns a ggplot object", { skip_if_not_installed("rstanarm") expect_gg(mcmc_parcoord(post, pars = c("wt", "am", "sigma"), np = np))