Skip to content

Commit 306c92e

Browse files
authored
Merge pull request #402 from stan-dev/mcmc_dots
adding `mcmc_dots` and `mcmc_dots_by_chain` functions
2 parents d831b30 + a564778 commit 306c92e

File tree

12 files changed

+1940
-18
lines changed

12 files changed

+1940
-18
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export(mcmc_dens)
7474
export(mcmc_dens_chains)
7575
export(mcmc_dens_chains_data)
7676
export(mcmc_dens_overlay)
77+
export(mcmc_dots)
78+
export(mcmc_dots_by_chain)
7779
export(mcmc_hex)
7880
export(mcmc_hist)
7981
export(mcmc_hist_by_chain)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# bayesplot (development version)
22

3+
* New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402)
4+
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
5+
36
# bayesplot 1.15.0
47

58
* Add `shape` argument to `mcmc_scatter` by @behramulukir (#375)

R/mcmc-distributions.R

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @template args-transformations
1313
#' @template args-facet_args
1414
#' @template args-density-controls
15-
#' @param ... Currently ignored.
15+
#' @param ... For dot plots, optional additional arguments to pass to [ggdist::stat_dots()].
1616
#' @param alpha Passed to the geom to control the transparency.
1717
#'
1818
#' @template return-ggplot
@@ -25,13 +25,19 @@
2525
#' \item{`mcmc_dens()`}{
2626
#' Kernel density plots of posterior draws with all chains merged.
2727
#' }
28+
#' \item{`mcmc_dots()`}{
29+
#' Dot plots of posterior draws with all chains merged.
30+
#' }
2831
#' \item{`mcmc_hist_by_chain()`}{
2932
#' Histograms of posterior draws with chains separated via faceting.
3033
#' }
3134
#' \item{`mcmc_dens_overlay()`}{
3235
#' Kernel density plots of posterior draws with chains separated but
3336
#' overlaid on a single plot.
3437
#' }
38+
#' \item{`mcmc_dots_by_chain()`}{
39+
#' Dot plots of posterior draws with chains separated via faceting.
40+
#' }
3541
#' \item{`mcmc_violin()`}{
3642
#' The density estimate of each chain is plotted as a violin with
3743
#' horizontal lines at notable quantiles.
@@ -77,14 +83,14 @@
7783
#' mcmc_hist(x, transformations = list(sigma = log))
7884
#'
7985
#' # separate histograms by chain
80-
#' color_scheme_set("pink")
86+
#' color_scheme_set("orange")
8187
#' mcmc_hist_by_chain(x, regex_pars = "beta")
8288
#' }
8389
#'
8490
#' #################
8591
#' ### Densities ###
8692
#' #################
87-
#'
93+
#' color_scheme_set("purple")
8894
#' mcmc_dens(x, pars = c("sigma", "beta[2]"),
8995
#' facet_args = list(nrow = 2))
9096
#' \donttest{
@@ -98,7 +104,26 @@
98104
#' }
99105
#' # separate chains as violin plots
100106
#' color_scheme_set("green")
101-
#' mcmc_violin(x) + panel_bg(color = "gray20", size = 2, fill = "gray30")
107+
#' mcmc_violin(x) + panel_bg(color = "gray20", linewidth = 2, fill = "gray30")
108+
#'
109+
#'
110+
#' #################
111+
#' ### Dot Plots ###
112+
#' #################
113+
#'
114+
#' # dot plots of some parameters
115+
#' color_scheme_set("pink")
116+
#' mcmc_dots(x, pars = c("alpha", "beta[2]"))
117+
#'
118+
#' \donttest{
119+
#' color_scheme_set("teal")
120+
#' # separate dot plots by chain
121+
#' mcmc_dots_by_chain(x, regex_pars = "beta")
122+
#'
123+
#' # custom facet labels (will change row labels to e.g. "Chain: 1" instead of just "1")
124+
#' chain_labeller <- ggplot2::labeller(.rows = ggplot2::label_both)
125+
#' mcmc_dots_by_chain(x, regex_pars = "beta", facet_args = list(labeller = chain_labeller))
126+
#' }
102127
#'
103128
NULL
104129

@@ -371,7 +396,67 @@ mcmc_violin <- function(
371396
)
372397
}
373398

399+
#' @rdname MCMC-distributions
400+
#' @export
401+
#' @template args-dots
402+
mcmc_dots <- function(
403+
x,
404+
pars = character(),
405+
regex_pars = character(),
406+
transformations = list(),
407+
...,
408+
facet_args = list(),
409+
binwidth = NA,
410+
alpha = 1,
411+
quantiles = 100
412+
) {
413+
check_ignored_arguments(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))
414+
415+
suggested_package("ggdist")
374416

417+
.mcmc_dots(
418+
x,
419+
pars = pars,
420+
regex_pars = regex_pars,
421+
transformations = transformations,
422+
binwidth = binwidth,
423+
facet_args = facet_args,
424+
alpha = alpha,
425+
quantiles = quantiles,
426+
...
427+
)
428+
}
429+
430+
#' @rdname MCMC-distributions
431+
#' @export
432+
mcmc_dots_by_chain <- function(
433+
x,
434+
pars = character(),
435+
regex_pars = character(),
436+
transformations = list(),
437+
...,
438+
facet_args = list(),
439+
binwidth = NA,
440+
alpha = 1,
441+
quantiles = 100
442+
) {
443+
check_ignored_arguments(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))
444+
445+
suggested_package("ggdist")
446+
447+
.mcmc_dots(
448+
x,
449+
pars = pars,
450+
regex_pars = regex_pars,
451+
transformations = transformations,
452+
binwidth = binwidth,
453+
facet_args = facet_args,
454+
by_chain = TRUE,
455+
alpha = alpha,
456+
quantiles = quantiles,
457+
...
458+
)
459+
}
375460

376461

377462
# internal -----------------------------------------------------------------
@@ -558,3 +643,65 @@ mcmc_violin <- function(
558643
yaxis_title(on = n_param == 1 && violin) +
559644
xaxis_title(on = n_param == 1)
560645
}
646+
647+
648+
.mcmc_dots <- function(
649+
x,
650+
pars = character(),
651+
regex_pars = character(),
652+
transformations = list(),
653+
facet_args = list(),
654+
binwidth = NA,
655+
by_chain = FALSE,
656+
alpha = 1,
657+
quantiles = NA,
658+
...
659+
) {
660+
x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
661+
662+
if (by_chain && !has_multiple_chains(x)) {
663+
STOP_need_multiple_chains()
664+
}
665+
666+
data <- melt_mcmc(x, value.name = "value")
667+
n_param <- num_params(data)
668+
669+
graph <- ggplot(data, aes(x = .data$value)) +
670+
ggdist::stat_dots(
671+
binwidth = binwidth,
672+
quantiles = quantiles,
673+
fill = get_color("mid"),
674+
color = get_color("mid_highlight"),
675+
alpha = alpha,
676+
...
677+
)
678+
679+
facet_args[["scales"]] <- facet_args[["scales"]] %||% "free"
680+
if (!by_chain) {
681+
if (n_param > 1) {
682+
facet_args[["facets"]] <- vars(.data$Parameter)
683+
graph <- graph + do.call("facet_wrap", facet_args)
684+
}
685+
} else {
686+
facet_args[["rows"]] <- vars(.data$Chain)
687+
if (n_param > 1) {
688+
facet_args[["cols"]] <- vars(.data$Parameter)
689+
}
690+
graph <- graph +
691+
do.call("facet_grid", facet_args) +
692+
force_x_axis_in_facets()
693+
}
694+
695+
if (n_param == 1) {
696+
graph <- graph + xlab(levels(data$Parameter))
697+
}
698+
699+
graph +
700+
dont_expand_y_axis(c(0.005, 0)) +
701+
bayesplot_theme_get() +
702+
yaxis_text(FALSE) +
703+
yaxis_title(FALSE) +
704+
yaxis_ticks(FALSE) +
705+
theme(axis.line.y = element_blank()) +
706+
xaxis_title(on = n_param == 1)
707+
}

R/ppc-distributions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ ppc_dots <-
532532
yrep,
533533
...,
534534
binwidth = NA,
535-
quantiles = NA,
535+
quantiles = 100,
536536
freq = TRUE) {
537537
check_ignored_arguments(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))
538538

R/ppd-distributions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ ppd_dots <-
199199
function(ypred,
200200
...,
201201
binwidth = NA,
202-
quantiles = NA,
202+
quantiles = 100,
203203
freq = TRUE) {
204204
check_ignored_arguments(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))
205205

man-roxygen/args-dots.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#' @param quantiles For dot plots, an optional integer passed to
22
#' [ggdist::stat_dots()] specifying the number of quantiles to use for a
3-
#' quantile dot plot. If `quantiles` is `NA` (the default) then all data
4-
#' points are plotted.
3+
#' quantile dot plot. If `quantiles` is `NA` then all data points are plotted.
4+
#' The default is `quantiles=100` so that each dot represent one percent of
5+
#' posterior mass.

man/MCMC-distributions.Rd

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

man/PPC-distributions.Rd

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

0 commit comments

Comments
 (0)