Skip to content

Commit 52f1a9f

Browse files
jgabrytjmahr
andauthored
Remove extra space in mcmc_areas (#230)
* remove extra space from mcmc_areas fixes #218 * add example of adjusting spacing see #218 * `mcmc_areas()` try to prevent top plotting from clipping * fix copy-paste error in NEWS Co-authored-by: TJ Mahr <[email protected]>
1 parent e54367b commit 52f1a9f

14 files changed

+430
-340
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Suggests:
4949
shinystan (>= 2.3.0),
5050
testthat (>= 2.0.0),
5151
vdiffr
52-
RoxygenNote: 7.1.0.9000
52+
RoxygenNote: 7.1.1
5353
VignetteBuilder: knitr
5454
Encoding: UTF-8
5555
Roxygen: list(markdown = TRUE)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Added missing `facet_args` argument to `mcmc_rank_overlay()`. (#221, @hhau)
1616
* Size of points and interval lines can set in
1717
`mcmc_intervals(..., outer_size, inner_size, point_size)`. (#215, #228, #229)
18+
* `mcmc_areas()` tries to use less blank vertical blank space. (#218, #230)
1819

1920

2021
# bayesplot 1.7.2

R/mcmc-intervals.R

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#'
5656
#' @examples
5757
#' set.seed(9262017)
58+
#'
59+
#' # load ggplot2 to use its functions to modify our plots
60+
#' library(ggplot2)
61+
#'
5862
#' # some parameter draws to use for demonstration
5963
#' x <- example_mcmc_draws(params = 6)
6064
#' dim(x)
@@ -64,19 +68,31 @@
6468
#' mcmc_intervals(x)
6569
#' mcmc_intervals(x, pars = c("beta[1]", "beta[2]"))
6670
#' mcmc_areas(x, regex_pars = "beta\\[[1-3]\\]", prob = 0.8) +
67-
#' ggplot2::labs(
71+
#' labs(
6872
#' title = "Posterior distributions",
6973
#' subtitle = "with medians and 80% intervals"
7074
#' )
7175
#'
7276
#' color_scheme_set("red")
73-
#' mcmc_areas(
77+
#' p <- mcmc_areas(
7478
#' x,
7579
#' pars = c("alpha", "beta[4]"),
7680
#' prob = 2/3,
7781
#' prob_outer = 0.9,
7882
#' point_est = "mean"
7983
#' )
84+
#' plot(p)
85+
#'
86+
#' # control spacing at top and bottom of plot
87+
#' # see ?ggplot2::expansion
88+
#' p + scale_y_discrete(
89+
#' limits = c("beta[4]", "alpha"),
90+
#' expand = expansion(add = c(1, 2))
91+
#' )
92+
#' p + scale_y_discrete(
93+
#' limits = c("beta[4]", "alpha"),
94+
#' expand = expansion(add = c(.1, .3))
95+
#' )
8096
#'
8197
#' # color by rhat value
8298
#' color_scheme_set("blue")
@@ -97,22 +113,22 @@
97113
#' b3 <- c("beta[1]", "beta[2]", "beta[3]")
98114
#'
99115
#' mcmc_areas(x, pars = b3, area_method = "equal area") +
100-
#' ggplot2::labs(
116+
#' labs(
101117
#' title = "Curves have same area",
102-
#' subtitle =
103-
#' "A wide, uncertain interval is spread thin when areas are equal")
118+
#' subtitle = "A wide, uncertain interval is spread thin when areas are equal"
119+
#' )
104120
#'
105121
#' mcmc_areas(x, pars = b3, area_method = "equal height") +
106-
#' ggplot2::labs(
122+
#' labs(
107123
#' title = "Curves have same maximum height",
108-
#' subtitle =
109-
#' "Local curvature is clearer but more uncertain curves use more area")
124+
#' subtitle = "Local curvature is clearer but more uncertain curves use more area"
125+
#' )
110126
#'
111127
#' mcmc_areas(x, pars = b3, area_method = "scaled height") +
112-
#' ggplot2::labs(
128+
#' labs(
113129
#' title = "Same maximum heights but heights scaled by square-root",
114-
#' subtitle =
115-
#' "Compromise: Local curvature is accentuated and less area is used")
130+
#' subtitle = "Compromise: Local curvature is accentuated and less area is used"
131+
#' )
116132
#'
117133
#' \donttest{
118134
#' # apply transformations
@@ -148,7 +164,7 @@
148164
#' # plotted with ridgelines
149165
#' m <- shinystan::eight_schools@posterior_sample
150166
#' mcmc_areas_ridges(m, pars = "mu", regex_pars = "theta") +
151-
#' ggplot2::ggtitle("Treatment effect on eight schools (Rubin, 1981)")
167+
#' ggtitle("Treatment effect on eight schools (Rubin, 1981)")
152168
#' }
153169
#'
154170
NULL
@@ -273,7 +289,8 @@ mcmc_areas <- function(x,
273289
x, pars, regex_pars, transformations,
274290
prob = prob, prob_outer = prob_outer,
275291
point_est = point_est, rhat = rhat,
276-
bw = bw, adjust = adjust, kernel = kernel, n_dens = n_dens)
292+
bw = bw, adjust = adjust, kernel = kernel, n_dens = n_dens
293+
)
277294
datas <- split(data, data$interval)
278295

279296
# Use a dummy empty dataframe if no point estimate
@@ -316,7 +333,11 @@ mcmc_areas <- function(x,
316333

317334
datas$bottom <- datas$outer %>%
318335
group_by(!!! groups) %>%
319-
summarise(ll = min(.data$x), hh = max(.data$x)) %>%
336+
summarise(
337+
ll = min(.data$x),
338+
hh = max(.data$x),
339+
.groups = "drop_last"
340+
) %>%
320341
ungroup()
321342

322343
args_bottom <- list(
@@ -358,9 +379,16 @@ mcmc_areas <- function(x,
358379
args_outer$color <- get_color("dark")
359380
}
360381

382+
# An invisible layer that is 2.5% taller than the plotted one
383+
args_outer2 <- args_outer
384+
args_outer2$mapping <- args_outer2$mapping %>%
385+
modify_aes_(scale = .925)
386+
args_outer2$color <- NA
387+
361388
layer_bottom <- do.call(geom_segment, args_bottom)
362389
layer_inner <- do.call(ggridges::geom_ridgeline, args_inner)
363390
layer_outer <- do.call(ggridges::geom_ridgeline, args_outer)
391+
layer_outer2 <- do.call(ggridges::geom_ridgeline, args_outer2)
364392

365393
point_geom <- if (no_point_est) {
366394
geom_ignore
@@ -384,12 +412,17 @@ mcmc_areas <- function(x,
384412
layer_inner +
385413
layer_point +
386414
layer_outer +
415+
layer_outer2 +
387416
layer_bottom +
388417
scale_color +
389418
scale_fill +
390419
scale_y_discrete(
391420
limits = unique(rev(data$parameter)),
392-
expand = expansion(add = c(0, .1), mult = c(.1, .3))) +
421+
expand = expansion(
422+
add = c(0, .5 + 1/(2 * nlevels(data$parameter))),
423+
mult = c(.1, .1)
424+
)
425+
) +
393426
xlim(x_lim) +
394427
bayesplot_theme_get() +
395428
legend_move(ifelse(color_by_rhat, "top", "none")) +

man/MCMC-intervals.Rd

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

man/tidy-params.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/figs/deps.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
- vdiffr-svg-engine: 1.0
2-
- vdiffr: 0.3.1
2+
- vdiffr: 0.3.2.2
33
- freetypeharfbuzz: 0.2.5

tests/figs/mcmc-intervals/mcmc-areas-default.svg

Lines changed: 45 additions & 40 deletions
Loading

tests/figs/mcmc-intervals/mcmc-areas-equal-height.svg

Lines changed: 45 additions & 40 deletions
Loading

tests/figs/mcmc-intervals/mcmc-areas-inner.svg

Lines changed: 45 additions & 40 deletions
Loading

tests/figs/mcmc-intervals/mcmc-areas-means.svg

Lines changed: 45 additions & 40 deletions
Loading

0 commit comments

Comments
 (0)