Stan Logo


Plotting Bayesian models

bayesplot is an R package providing an extensive library of plotting functions for use after fitting Bayesian models (typically with MCMC). Currently bayesplot offers a variety of plots of posterior draws, visual MCMC diagnostics, as well as graphical posterior predictive checking. Additional functionality (e.g. for forecasting/out-of-sample prediction and other inference-related tasks) will be added in future releases.

The plots created by bayesplot are ggplot objects, which means that after a plot is created it can be further customized using the various functions for modifying ggplot objects provided by the ggplot2 package.

The idea behind bayesplot is not only to provide convenient functionality for users, but also a common set of functions that can be easily used by developers working on a variety of packages for Bayesian modeling, particularly (but not necessarily) those powered by RStan.

Getting Started

If you are just getting started with bayesplot we recommend starting with the tutorial vignettes. There are also many examples throughout the package documentation.


Install the latest release from CRAN:


Install the latest development version from GitHub:

if (!require("devtools")) {
devtools::install_github("stan-dev/bayesplot", dependencies = TRUE, build_vignettes = TRUE)

You can also set build_vignettes=FALSE for a faster installation from GitHub (the vignettes can always be accessed online anytime at For issues related to pandoc see the Readme


Some quick examples using MCMC draws obtained from our rstanarm and rstan packages.


fit <- stan_glm(mpg ~ ., data = mtcars)
posterior <- as.matrix(fit)

plot_title <- ggtitle("Posterior distributions",
                      "with medians and 80% intervals")
           pars = c("cyl", "drat", "am", "wt"),
           prob = 0.8) + plot_title
# with rstan demo model
fit2 <- stan_demo("eight_schools", warmup = 300, iter = 700)
posterior2 <- extract(fit2, inc_warmup = TRUE, permuted = FALSE)

p <- mcmc_trace(posterior2,  pars = c("mu", "tau"), n_warmup = 300,
                facet_args = list(nrow = 2, labeller = label_parsed))
p + facet_text(size = 15)
# scatter plot also showing divergences
  pars = c("tau", "theta[1]"), 
  np = nuts_params(fit2), 
  np_style = scatter_style_np(div_color = "green", div_alpha = 0.8)
np <- nuts_params(fit2)
mcmc_nuts_energy(np) + ggtitle("NUTS Energy Diagnostic")