From c953ff7b2ab0a75081ff345e14d6298484712582 Mon Sep 17 00:00:00 2001 From: ishaan-arora-1 Date: Tue, 10 Mar 2026 20:49:07 +0530 Subject: [PATCH 1/4] Replace stat_function() with geom_function() and fix override.aes size - overlay_function() now wraps geom_function() instead of the older stat_function(), matching modern ggplot2 conventions - Use linewidth instead of size in override.aes for line-based legend keys in mcmc-traces.R and ppd-distributions.R - Update documentation and tests accordingly Closes #449 --- R/bayesplot-helpers.R | 6 +++--- R/mcmc-traces.R | 2 +- R/ppd-distributions.R | 2 +- man/bayesplot-helpers.Rd | 4 ++-- tests/testthat/test-convenience-functions.R | 3 +-- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/R/bayesplot-helpers.R b/R/bayesplot-helpers.R index d3d86ec38..52f47a119 100644 --- a/R/bayesplot-helpers.R +++ b/R/bayesplot-helpers.R @@ -21,7 +21,7 @@ #' [ggplot2::element_line()]. #' #' For `overlay_function`, `...` is passed to -#' [ggplot2::stat_function()]. +#' [ggplot2::geom_function()]. #' #' @return #' A **ggplot2** layer or [ggplot2::theme()] object that can be @@ -91,7 +91,7 @@ #' } #' #' \subsection{Superimpose a function on an existing plot}{ -#' * `overlay_function()` is a simple wrapper for [ggplot2::stat_function()] but +#' * `overlay_function()` is a simple wrapper for [ggplot2::geom_function()] but #' with the `inherit.aes` argument fixed to `FALSE`. Fixing `inherit.aes=FALSE` #' will avoid potential errors due to the [ggplot2::aes()]thetic mapping used by #' certain **bayesplot** plotting functions. @@ -467,7 +467,7 @@ grid_lines_y <- function(color = "gray50", size = 0.2) { #' @rdname bayesplot-helpers #' @export overlay_function <- function(...) { - stat_function(..., inherit.aes = FALSE) + geom_function(..., inherit.aes = FALSE) } diff --git a/R/mcmc-traces.R b/R/mcmc-traces.R index cad857aef..9df18f6ed 100644 --- a/R/mcmc-traces.R +++ b/R/mcmc-traces.R @@ -719,7 +719,7 @@ mcmc_trace_data <- function(x, color = guide_legend(order = 1), linetype = guide_legend( order = 2, title = NULL, keywidth = rel(1/2), - override.aes = list(size = rel(1/2))) + override.aes = list(linewidth = rel(1/2))) ) } } diff --git a/R/ppd-distributions.R b/R/ppd-distributions.R index 70c4e5a68..a16c7e459 100644 --- a/R/ppd-distributions.R +++ b/R/ppd-distributions.R @@ -66,7 +66,7 @@ ppd_dens_overlay <- scale_color_ppd( values = get_color("m"), guide = guide_legend( # in case user turns legend back on - override.aes = list(size = 2 * size, alpha = 1)) + override.aes = list(linewidth = 2 * size, alpha = 1)) ) + bayesplot_theme_get() + dont_expand_axes() + diff --git a/man/bayesplot-helpers.Rd b/man/bayesplot-helpers.Rd index 897bce928..3e378a7b8 100644 --- a/man/bayesplot-helpers.Rd +++ b/man/bayesplot-helpers.Rd @@ -90,7 +90,7 @@ For \code{xaxis_ticks} and \code{yaxis_ticks}, \code{...} is passed to \code{\link[ggplot2:element]{ggplot2::element_line()}}. For \code{overlay_function}, \code{...} is passed to -\code{\link[ggplot2:geom_function]{ggplot2::stat_function()}}.} +\code{\link[ggplot2:geom_function]{ggplot2::geom_function()}}.} \item{na.rm}{A logical scalar passed to the appropriate geom (e.g. \code{\link[ggplot2:geom_abline]{ggplot2::geom_vline()}}). The default is \code{TRUE}.} @@ -189,7 +189,7 @@ an existing plot (ggplot object) to add grid lines to the plot background. \subsection{Superimpose a function on an existing plot}{ \itemize{ -\item \code{overlay_function()} is a simple wrapper for \code{\link[ggplot2:geom_function]{ggplot2::stat_function()}} but +\item \code{overlay_function()} is a simple wrapper for \code{\link[ggplot2:geom_function]{ggplot2::geom_function()}} but with the \code{inherit.aes} argument fixed to \code{FALSE}. Fixing \code{inherit.aes=FALSE} will avoid potential errors due to the \code{\link[ggplot2:aes]{ggplot2::aes()}}thetic mapping used by certain \strong{bayesplot} plotting functions. diff --git a/tests/testthat/test-convenience-functions.R b/tests/testthat/test-convenience-functions.R index 7b197f279..83ec7e912 100644 --- a/tests/testthat/test-convenience-functions.R +++ b/tests/testthat/test-convenience-functions.R @@ -180,9 +180,8 @@ test_that("yaxis_ticks returns correct theme object", { # overlay functions ------------------------------------------------------- test_that("overlay_function returns the correct object", { - expect_error(overlay_function(), 'argument "fun" is missing') a <- overlay_function(fun = "dnorm") - b <- stat_function(fun = "dnorm", inherit.aes = FALSE) + b <- geom_function(fun = "dnorm", inherit.aes = FALSE) a$constructor <- b$constructor <- NULL expect_equal(a, b, ignore_function_env = TRUE) }) From e3b1c3212cbd0daffe413053a18113adc543230e Mon Sep 17 00:00:00 2001 From: ishaan-arora-1 Date: Tue, 10 Mar 2026 20:58:57 +0530 Subject: [PATCH 2/4] Drop ppd-distributions.R change (already covered by PR #430) --- R/ppd-distributions.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/ppd-distributions.R b/R/ppd-distributions.R index a16c7e459..70c4e5a68 100644 --- a/R/ppd-distributions.R +++ b/R/ppd-distributions.R @@ -66,7 +66,7 @@ ppd_dens_overlay <- scale_color_ppd( values = get_color("m"), guide = guide_legend( # in case user turns legend back on - override.aes = list(linewidth = 2 * size, alpha = 1)) + override.aes = list(size = 2 * size, alpha = 1)) ) + bayesplot_theme_get() + dont_expand_axes() + From 024740f04978db7a122aebc424f383317b8103e4 Mon Sep 17 00:00:00 2001 From: ishaan-arora-1 Date: Wed, 11 Mar 2026 01:35:56 +0530 Subject: [PATCH 3/4] Delete stale vdiffr snapshots for mcmc_trace divergence plots The override.aes change from size to linewidth affects the legend key rendering, so these snapshots need to be regenerated. --- .../mcmc-trace-divergences-custom.svg | 111 ------------------ .../mcmc-trace-divergences-default.svg | 111 ------------------ 2 files changed, 222 deletions(-) delete mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg delete mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg deleted file mode 100644 index b856f48d1..000000000 --- a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --2 -0 -2 - - - - - - - - - - -0 -100 -200 -300 -400 -500 -V1 -Chain - - - - -1 -2 -3 -4 - -Divergence -mcmc_trace divergences (custom) - - diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg deleted file mode 100644 index 2063b26eb..000000000 --- a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --2 -0 -2 - - - - - - - - - - -0 -100 -200 -300 -400 -500 -V1 -Chain - - - - -1 -2 -3 -4 - -Divergence -mcmc_trace divergences (default) - - From d99e0d2b84db67a93e0e6b37fd2f723f81ca7db6 Mon Sep 17 00:00:00 2001 From: ishaan-arora-1 Date: Wed, 11 Mar 2026 01:43:27 +0530 Subject: [PATCH 4/4] Add regenerated vdiffr snapshots for mcmc_trace divergence plots --- .../mcmc-trace-divergences-custom.svg | 111 ++++++++++++++++++ .../mcmc-trace-divergences-default.svg | 111 ++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg create mode 100644 tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg new file mode 100644 index 000000000..b7b9d0b5d --- /dev/null +++ b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-custom.svg @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-2 +0 +2 + + + + + + + + + + +0 +100 +200 +300 +400 +500 +V1 +Chain + + + + +1 +2 +3 +4 + +Divergence +mcmc_trace divergences (custom) + + diff --git a/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg new file mode 100644 index 000000000..0bd292caf --- /dev/null +++ b/tests/testthat/_snaps/mcmc-traces/mcmc-trace-divergences-default.svg @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +-2 +0 +2 + + + + + + + + + + +0 +100 +200 +300 +400 +500 +V1 +Chain + + + + +1 +2 +3 +4 + +Divergence +mcmc_trace divergences (default) + +