diff --git a/R/plot_posterior.R b/R/plot_posterior.R index 24fadc6..b8c726e 100644 --- a/R/plot_posterior.R +++ b/R/plot_posterior.R @@ -40,6 +40,8 @@ #' `upper` and will contain the overflow. Use this arg to specify either a #' single value to be used for all params, or a named numeric vector whose #' names are params. +#' @param nrow,ncol Number of rows and columns for how the individual plots for +#' each parameter are arranged into a grid. Passed to [ggplot2::facet_wrap()]. #' @param skip_stanfit_to_dt If this argument is set to `TRUE`, the #' `posterior_samples` argument (and the `prior_samples` argument if used) #' should be used to provide a datatable of samples instead of a stanfit @@ -65,6 +67,8 @@ plot_posterior <- function(posterior_samples, labels = NA, lower = NA, upper = NA, + nrow = NA, + ncol = NA, skip_stanfit_to_dt = NA, bins = 30) { @@ -79,6 +83,11 @@ plot_posterior <- function(posterior_samples, check_numeric(bins, lower = 1) + # Recode defaults for nrow and ncol (NA, chosen so that they appear in the + # docs here) to NULL (their defaults in facet_wrap) + if (identical(NA, nrow)) nrow <- NULL + if (identical(NA, ncol)) ncol <- NULL + # Get the posterior samples into a dt with only the desired params if (identical(skip_stanfit_to_dt, NA)) { skip_stanfit_to_dt <- FALSE @@ -334,7 +343,10 @@ plot_posterior <- function(posterior_samples, } plot <- ggplot2::ggplot(dt) + - ggplot2::facet_wrap(~ param, scales = "free") + + ggplot2::facet_wrap(~ param, + scales = "free", + nrow = nrow, + ncol = ncol) + ggplot2::theme_classic() + ggplot2::coord_cartesian(expand = FALSE) if (have_prior) { diff --git a/man/plot_posterior.Rd b/man/plot_posterior.Rd index f0f6c0b..26c0c91 100644 --- a/man/plot_posterior.Rd +++ b/man/plot_posterior.Rd @@ -13,6 +13,8 @@ plot_posterior( labels = NA, lower = NA, upper = NA, + nrow = NA, + ncol = NA, skip_stanfit_to_dt = NA, bins = 30 ) @@ -64,6 +66,9 @@ sampled values that are greater than \code{upper} are decreased to exactly equal single value to be used for all params, or a named numeric vector whose names are params.} +\item{nrow, ncol}{Number of rows and columns for how the individual plots for +each parameter are arranged into a grid. Passed to \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}}.} + \item{skip_stanfit_to_dt}{If this argument is set to \code{TRUE}, the \code{posterior_samples} argument (and the \code{prior_samples} argument if used) should be used to provide a datatable of samples instead of a stanfit diff --git a/tests/testthat/test-plot_posterior.R b/tests/testthat/test-plot_posterior.R index 037ec27..9fc4f3b 100644 --- a/tests/testthat/test-plot_posterior.R +++ b/tests/testthat/test-plot_posterior.R @@ -288,5 +288,6 @@ if (do_manual_visual_testing) { plot_posterior(posterior_dt, prior_samples = prior_dt, upper = 10, skip_stanfit_to_dt = TRUE) plot_posterior(posterior_dt, prior_samples = prior_dt, lower = 2, skip_stanfit_to_dt = TRUE) plot_posterior(posterior_dt, prior_samples = prior_dt, lower = 2, upper = 10, skip_stanfit_to_dt = TRUE) + plot_posterior(posterior, nrow = 2) }