55
55
# '
56
56
# ' @examples
57
57
# ' set.seed(9262017)
58
+ # '
59
+ # ' # load ggplot2 to use its functions to modify our plots
60
+ # ' library(ggplot2)
61
+ # '
58
62
# ' # some parameter draws to use for demonstration
59
63
# ' x <- example_mcmc_draws(params = 6)
60
64
# ' dim(x)
64
68
# ' mcmc_intervals(x)
65
69
# ' mcmc_intervals(x, pars = c("beta[1]", "beta[2]"))
66
70
# ' mcmc_areas(x, regex_pars = "beta\\[[1-3]\\]", prob = 0.8) +
67
- # ' ggplot2:: labs(
71
+ # ' labs(
68
72
# ' title = "Posterior distributions",
69
73
# ' subtitle = "with medians and 80% intervals"
70
74
# ' )
71
75
# '
72
76
# ' color_scheme_set("red")
73
- # ' mcmc_areas(
77
+ # ' p <- mcmc_areas(
74
78
# ' x,
75
79
# ' pars = c("alpha", "beta[4]"),
76
80
# ' prob = 2/3,
77
81
# ' prob_outer = 0.9,
78
82
# ' point_est = "mean"
79
83
# ' )
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
+ # ' )
80
96
# '
81
97
# ' # color by rhat value
82
98
# ' color_scheme_set("blue")
97
113
# ' b3 <- c("beta[1]", "beta[2]", "beta[3]")
98
114
# '
99
115
# ' mcmc_areas(x, pars = b3, area_method = "equal area") +
100
- # ' ggplot2:: labs(
116
+ # ' labs(
101
117
# ' 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
+ # ' )
104
120
# '
105
121
# ' mcmc_areas(x, pars = b3, area_method = "equal height") +
106
- # ' ggplot2:: labs(
122
+ # ' labs(
107
123
# ' 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
+ # ' )
110
126
# '
111
127
# ' mcmc_areas(x, pars = b3, area_method = "scaled height") +
112
- # ' ggplot2:: labs(
128
+ # ' labs(
113
129
# ' 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
+ # ' )
116
132
# '
117
133
# ' \donttest{
118
134
# ' # apply transformations
148
164
# ' # plotted with ridgelines
149
165
# ' m <- shinystan::eight_schools@posterior_sample
150
166
# ' 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)")
152
168
# ' }
153
169
# '
154
170
NULL
@@ -273,7 +289,8 @@ mcmc_areas <- function(x,
273
289
x , pars , regex_pars , transformations ,
274
290
prob = prob , prob_outer = prob_outer ,
275
291
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
+ )
277
294
datas <- split(data , data $ interval )
278
295
279
296
# Use a dummy empty dataframe if no point estimate
@@ -316,7 +333,11 @@ mcmc_areas <- function(x,
316
333
317
334
datas $ bottom <- datas $ outer %> %
318
335
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
+ ) %> %
320
341
ungroup()
321
342
322
343
args_bottom <- list (
@@ -358,9 +379,16 @@ mcmc_areas <- function(x,
358
379
args_outer $ color <- get_color(" dark" )
359
380
}
360
381
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
+
361
388
layer_bottom <- do.call(geom_segment , args_bottom )
362
389
layer_inner <- do.call(ggridges :: geom_ridgeline , args_inner )
363
390
layer_outer <- do.call(ggridges :: geom_ridgeline , args_outer )
391
+ layer_outer2 <- do.call(ggridges :: geom_ridgeline , args_outer2 )
364
392
365
393
point_geom <- if (no_point_est ) {
366
394
geom_ignore
@@ -384,12 +412,17 @@ mcmc_areas <- function(x,
384
412
layer_inner +
385
413
layer_point +
386
414
layer_outer +
415
+ layer_outer2 +
387
416
layer_bottom +
388
417
scale_color +
389
418
scale_fill +
390
419
scale_y_discrete(
391
420
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
+ ) +
393
426
xlim(x_lim ) +
394
427
bayesplot_theme_get() +
395
428
legend_move(ifelse(color_by_rhat , " top" , " none" )) +
0 commit comments