Skip to content

Commit f791f30

Browse files
committed
bayesplot_grid: add subtitles arg
1 parent 7d9b2ad commit f791f30

File tree

4 files changed

+46
-32
lines changed

4 files changed

+46
-32
lines changed

R/bayesplot_grid.R

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#' @param grid_args An optional named list of arguments to pass to
1111
#' \code{\link[gridExtra]{arrangeGrob}} (\code{nrow}, \code{ncol},
1212
#' \code{widths}, etc.).
13-
#' @param titles An optional character vector of plot titles. If specified,
14-
#' \code{titles} must must have length equal to the number of plots speficied.
13+
#' @param titles,subtitles Optional character vectors of plot titles and
14+
#' subtitles. If specified, \code{titles} and \code{subtitles} must must have
15+
#' length equal to the number of plots speficied.
1516
#' @param xlim,ylim Optionally, numeric vectors of length 2 specifying lower and
1617
#' upper limits for the axes that will be shared across all plots.
1718
#' @param legends If any of the plots have legends should they be displayed?
@@ -53,16 +54,21 @@
5354
#' bayesplot_grid(ppc1, ppc2)
5455
#'
5556
#' # make sure the plots use the same limits for the axes
56-
#' bayesplot_grid(ppc1, ppc2, xlim = c(-5, 60), ylim = c(0, 0.15))
57+
#' bayesplot_grid(ppc1, ppc2, xlim = c(-5, 60), ylim = c(0, 0.2))
58+
#'
59+
#' # remove the legends and add text
60+
#' bayesplot_grid(ppc1, ppc2, xlim = c(-5, 60), ylim = c(0, 0.2),
61+
#' legends = FALSE, subtitles = rep("Predicted MPG", 2))
5762
#' }
5863
#'
5964
bayesplot_grid <-
6065
function(...,
6166
plots = list(),
62-
grid_args = list(),
63-
titles = character(),
6467
xlim = NULL,
6568
ylim = NULL,
69+
grid_args = list(),
70+
titles = character(),
71+
subtitles = character(),
6672
legends = TRUE) {
6773

6874
suggested_package("gridExtra")
@@ -85,6 +91,11 @@ bayesplot_grid <-
8591
plots <- lapply(seq_along(plots), function(j)
8692
plots[[j]] + ggtitle(titles[j]))
8793
}
94+
if (length(subtitles)) {
95+
stopifnot(is.character(subtitles), length(subtitles) == length(plots))
96+
plots <- lapply(seq_along(plots), function(j)
97+
plots[[j]] + labs(subtitle = subtitles[j]))
98+
}
8899
if (!legends)
89100
plots <- lapply(plots, function(p)
90101
p + legend_none())

man/bayesplot_grid.Rd

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-bayesplot_grid.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ test_that("bayesplot_grid throws correct errors", {
2323
"objects in '...' must be ggplot objects.")
2424
expect_error(bayesplot_grid(p1, p2, titles = c("plot1")),
2525
"length(titles) == length(plots) is not TRUE", fixed = TRUE)
26+
expect_error(bayesplot_grid(p1, p2, subtitles = c("plot1")),
27+
"length(subtitles) == length(plots) is not TRUE", fixed = TRUE)
2628
})
2729

2830
test_that("bayesplot_grid works", {
@@ -33,6 +35,7 @@ test_that("bayesplot_grid works", {
3335
expect_silent(
3436
b <- bayesplot_grid(plots = list(p1, p2),
3537
titles = c("plot1", "plot2"),
38+
subtitles = c("plot1_sub", "plot2_sub"),
3639
legends = FALSE)
3740
)
3841

vignettes/MCMC-diagnostics.Rmd

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ params:
1919

2020
```{r, pkgs, include=FALSE}
2121
library("ggplot2")
22-
library("gridExtra")
2322
library("rstan")
2423
```
2524

@@ -32,13 +31,11 @@ and graphical posterior predictive checks are covered in
3231
In addition to __bayesplot__ we'll load the following packages:
3332

3433
* __ggplot2__ for customizing the ggplot objects created by __bayesplot__
35-
* __gridExtra__ for displaying multiple ggplot objects in a grid
3634
* __rstan__ for fitting the example models used throughout the vignette
3735

3836
```{r, eval=FALSE}
3937
library("bayesplot")
40-
library("ggplot2")
41-
library("gridExtra")
38+
library("ggplot2")
4239
library("rstan")
4340
```
4441

@@ -267,14 +264,13 @@ Even for models fit using **rstan** the parameterization can make a big
267264
difference. Here are the $n_{eff}/N$ plots for `fit1` and `fit2` side by side.
268265

269266
```{r mcmc_neff-compare}
270-
# A function we'll use several times to plot comparisons of the centered
271-
# parameterization (cp) and the non-centered parameterization (ncp)
272-
compare_cp_ncp <- function(cp_plot, ncp_plot, ...) {
273-
grid.arrange(
274-
cp_plot + labs(subtitle = "Centered parameterization"),
275-
ncp_plot + labs(subtitle = "Non-centered parameterization"),
276-
...
277-
)
267+
# A function we'll use several times to plot comparisons of the centered
268+
# parameterization (cp) and the non-centered parameterization (ncp). See
269+
# help("bayesplot_grid") for details on the bayesplot_grid function used here.
270+
compare_cp_ncp <- function(cp_plot, ncp_plot, ncol = 1) {
271+
txt <- c("Centered parameterization", "Non-centered parameterization")
272+
bayesplot_grid(cp_plot, ncp_plot, subtitles = txt,
273+
grid_args = list(ncol = ncol))
278274
}
279275
280276
neff1 <- neff_ratio(fit1, pars = c("theta", "mu", "tau"))
@@ -300,9 +296,9 @@ is $\eta_1$ (and $\theta_1$ is later constructed from $\eta_1$, $\mu$, and
300296
$\tau$):
301297

302298
```{r mcmc_acf}
303-
grid.arrange(
299+
compare_cp_ncp(
304300
mcmc_acf(posterior1, pars = "theta[1]", lags = 10),
305-
mcmc_acf(posterior2, pars = "eta[1]", lags = 10),
301+
mcmc_acf(posterior2, pars = "eta[1]", lags = 10),
306302
ncol = 2
307303
)
308304
```
@@ -321,11 +317,9 @@ details on the concepts.
321317

322318
The special **bayesplot** functions for NUTS diagnostics are
323319

324-
* `mcmc_nuts_acceptance`
325-
* `mcmc_nuts_divergence`
326-
* `mcmc_nuts_stepsize`
327-
* `mcmc_nuts_treedepth`
328-
* `mcmc_nuts_energy`
320+
```{r, available_mcmc-nuts}
321+
available_mcmc(pattern = "_nuts_")
322+
```
329323

330324
The **bayesplot** package also provides generic functions `log_posterior` and
331325
`nuts_params` for extracting the required information for the plots from fitted

0 commit comments

Comments
 (0)