From 1665053972fef6538d3772ff0b55a6645c15d6d7 Mon Sep 17 00:00:00 2001 From: jgabry Date: Thu, 10 Sep 2020 09:39:36 -0600 Subject: [PATCH 1/3] add alpha argument to mcmc_hist and mcmc_dens --- R/mcmc-distributions.R | 20 +++++++++++++++----- man/MCMC-distributions.Rd | 11 ++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/R/mcmc-distributions.R b/R/mcmc-distributions.R index 63a63c95..4e44dfa7 100644 --- a/R/mcmc-distributions.R +++ b/R/mcmc-distributions.R @@ -12,6 +12,7 @@ #' @template args-transformations #' @template args-facet_args #' @param ... Currently ignored. +#' @param alpha Passed to the geom to control the transparency. #' #' @template return-ggplot #' @@ -113,7 +114,8 @@ mcmc_hist <- function(x, facet_args = list(), binwidth = NULL, breaks = NULL, - freq = TRUE) { + freq = TRUE, + alpha = 1) { check_ignored_arguments(...) .mcmc_hist( x, @@ -125,6 +127,7 @@ mcmc_hist <- function(x, breaks = breaks, by_chain = FALSE, freq = freq, + alpha = alpha, ... ) } @@ -137,7 +140,8 @@ mcmc_dens <- function(x, transformations = list(), ..., facet_args = list(), - trim = FALSE) { + trim = FALSE, + alpha = 1) { check_ignored_arguments(...) .mcmc_dens( x, @@ -147,6 +151,7 @@ mcmc_dens <- function(x, facet_args = facet_args, by_chain = FALSE, trim = trim, + alpha = alpha, ... ) } @@ -161,7 +166,8 @@ mcmc_hist_by_chain <- function(x, ..., facet_args = list(), binwidth = NULL, - freq = TRUE) { + freq = TRUE, + alpha = 1) { check_ignored_arguments(...) .mcmc_hist( x, @@ -172,6 +178,7 @@ mcmc_hist_by_chain <- function(x, binwidth = binwidth, by_chain = TRUE, freq = freq, + alpha = alpha, ... ) } @@ -312,6 +319,7 @@ mcmc_violin <- function(x, breaks = NULL, by_chain = FALSE, freq = TRUE, + alpha = 1, ...) { x <- prepare_mcmc_array(x, pars, regex_pars, transformations) @@ -330,7 +338,8 @@ mcmc_violin <- function(x, size = .25, na.rm = TRUE, binwidth = binwidth, - breaks = breaks + breaks = breaks, + alpha = alpha ) facet_args[["scales"]] <- facet_args[["scales"]] %||% "free" @@ -373,6 +382,7 @@ mcmc_violin <- function(x, geom = c("density", "violin"), probs = c(0.1, 0.5, 0.9), trim = FALSE, + alpha = 1, ...) { x <- prepare_mcmc_array(x, pars, regex_pars, transformations) data <- melt_mcmc(x) @@ -396,7 +406,7 @@ mcmc_violin <- function(x, } else { list(x = ~ Value) } - geom_args <- list(size = 0.5, na.rm = TRUE) + geom_args <- list(size = 0.5, na.rm = TRUE, alpha = alpha) if (violin) { geom_args[["draw_quantiles"]] <- probs } else { diff --git a/man/MCMC-distributions.Rd b/man/MCMC-distributions.Rd index 70385b96..2e68f103 100644 --- a/man/MCMC-distributions.Rd +++ b/man/MCMC-distributions.Rd @@ -20,7 +20,8 @@ mcmc_hist( facet_args = list(), binwidth = NULL, breaks = NULL, - freq = TRUE + freq = TRUE, + alpha = 1 ) mcmc_dens( @@ -30,7 +31,8 @@ mcmc_dens( transformations = list(), ..., facet_args = list(), - trim = FALSE + trim = FALSE, + alpha = 1 ) mcmc_hist_by_chain( @@ -41,7 +43,8 @@ mcmc_hist_by_chain( ..., facet_args = list(), binwidth = NULL, - freq = TRUE + freq = TRUE, + alpha = 1 ) mcmc_dens_overlay( @@ -151,6 +154,8 @@ plots the y-axis text is off by default. To view the count or density labels on the y-axis see the \code{\link[=yaxis_text]{yaxis_text()}} convenience function.)} +\item{alpha}{Passed to the geom to control the transparency.} + \item{trim}{A logical scalar passed to \code{\link[ggplot2:geom_density]{ggplot2::geom_density()}}.} \item{color_chains}{Option for whether to separately color chains.} From 255f31dc1e0eb49ccfaa29a2f68123294b06b81b Mon Sep 17 00:00:00 2001 From: jgabry Date: Fri, 23 Oct 2020 13:28:46 -0600 Subject: [PATCH 2/3] add visual tests --- NEWS.md | 2 + .../mcmc-distributions/mcmc-dens-alpha.svg | 209 +++++++++++ .../mcmc-distributions/mcmc-dens-default.svg | 209 +++++++++++ .../mcmc-distributions/mcmc-hist-alpha.svg | 345 ++++++++++++++++++ tests/testthat/test-mcmc-distributions.R | 14 + 5 files changed, 779 insertions(+) create mode 100644 tests/figs/mcmc-distributions/mcmc-dens-alpha.svg create mode 100644 tests/figs/mcmc-distributions/mcmc-dens-default.svg create mode 100644 tests/figs/mcmc-distributions/mcmc-hist-alpha.svg diff --git a/NEWS.md b/NEWS.md index 588ae62b..d346a0d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -31,6 +31,8 @@ density estimation. The new argument `boundary_correction` defaults to TRUE but can be set to FALSE to recover the old version of the plot. (#171, #235, @ecoronado92) + +* `mcmc_hist()` and `mcmc_dens()` gain argument `alpha` for controling transparency. (#244) # bayesplot 1.7.2 diff --git a/tests/figs/mcmc-distributions/mcmc-dens-alpha.svg b/tests/figs/mcmc-distributions/mcmc-dens-alpha.svg new file mode 100644 index 00000000..fb4f75c8 --- /dev/null +++ b/tests/figs/mcmc-distributions/mcmc-dens-alpha.svg @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +V4 + + + + + + + + + + +V5 + + + + + + + + + + + + + + + + + + + + +V1 + + + + + + + + + + +V2 + + + + + + + + + + +V3 + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + +-2 +0 +2 + + + + + + +-2 +-1 +0 +1 +2 + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + +-2 +-1 +0 +1 +2 +3 + + + + + +mcmc_dens (alpha) + diff --git a/tests/figs/mcmc-distributions/mcmc-dens-default.svg b/tests/figs/mcmc-distributions/mcmc-dens-default.svg new file mode 100644 index 00000000..d8e02cd0 --- /dev/null +++ b/tests/figs/mcmc-distributions/mcmc-dens-default.svg @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +V4 + + + + + + + + + + +V5 + + + + + + + + + + + + + + + + + + + + +V1 + + + + + + + + + + +V2 + + + + + + + + + + +V3 + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + +-2 +0 +2 + + + + + + +-2 +-1 +0 +1 +2 + + + + + + +-2 +-1 +0 +1 +2 + + + + + + + +-2 +-1 +0 +1 +2 +3 + + + + + +mcmc_dens (default) + diff --git a/tests/figs/mcmc-distributions/mcmc-hist-alpha.svg b/tests/figs/mcmc-distributions/mcmc-hist-alpha.svg new file mode 100644 index 00000000..3996a108 --- /dev/null +++ b/tests/figs/mcmc-distributions/mcmc-hist-alpha.svg @@ -0,0 +1,345 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +V4 + + + + + + + + + + +V5 + + + + + + + + + + + + + + + + + + + + +V1 + + + + + + + + + + +V2 + + + + + + + + + + +V3 + + + + + + + + + + + +-2 +-1 +0 +1 +2 + + + + +-2 +0 +2 + + + + + + + +-2 +-1 +0 +1 +2 +3 + + + + + + +-2 +-1 +0 +1 +2 + + + + +-2 +0 +2 + + + + + +mcmc_hist (alpha) + diff --git a/tests/testthat/test-mcmc-distributions.R b/tests/testthat/test-mcmc-distributions.R index b494ec60..bbcc2a0f 100644 --- a/tests/testthat/test-mcmc-distributions.R +++ b/tests/testthat/test-mcmc-distributions.R @@ -119,5 +119,19 @@ test_that("mcmc_hist renders correctly", { p_freq <- mcmc_hist(vdiff_dframe, freq = TRUE) vdiffr::expect_doppelganger("mcmc_hist (freq)", p_freq) + + p_alpha <- mcmc_hist(vdiff_dframe, alpha = 0) + vdiffr::expect_doppelganger("mcmc_hist (alpha)", p_alpha) +}) + +test_that("mcmc_dens renders correctly", { + testthat::skip_on_cran() + testthat::skip_if_not_installed("vdiffr") + + p_base <- mcmc_dens(vdiff_dframe) + vdiffr::expect_doppelganger("mcmc_dens (default)", p_base) + + p_alpha <- mcmc_dens(vdiff_dframe, alpha = 0) + vdiffr::expect_doppelganger("mcmc_dens (alpha)", p_alpha) }) From df8cc4e490ad2e4047c2e8796f7bf246efda70f7 Mon Sep 17 00:00:00 2001 From: jgabry Date: Tue, 5 Oct 2021 13:42:22 -0600 Subject: [PATCH 3/3] Update mcmc-distributions.R --- R/mcmc-distributions.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/mcmc-distributions.R b/R/mcmc-distributions.R index 44af0670..8bb32867 100644 --- a/R/mcmc-distributions.R +++ b/R/mcmc-distributions.R @@ -144,12 +144,12 @@ mcmc_dens <- function( transformations = list(), ..., facet_args = list(), - alpha = 1, trim = FALSE, bw = NULL, adjust = NULL, kernel = NULL, - n_dens = NULL + n_dens = NULL, + alpha = 1 ) { check_ignored_arguments(...) .mcmc_dens(