diff --git a/NEWS.md b/NEWS.md index a69d7e86..03326003 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 `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style. * Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()` # 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..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,8 +483,8 @@ 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)) + + 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() + space_legend_keys() + diff --git a/R/mcmc-diagnostics.R b/R/mcmc-diagnostics.R index 92c6fef4..87e716da 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) @@ -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) + @@ -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) + @@ -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) + @@ -572,7 +572,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..dd4adb38 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() + @@ -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-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) } 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) + 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))