Skip to content

Commit a30a706

Browse files
authored
Merge pull request #485 from utkarshpawade/fix/use-expansion-helper-in-scale-expand
Replace c(mult, add) with expansion() in scale expand args
2 parents b98d1e3 + bf8996e commit a30a706

12 files changed

+48
-20
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402)
1212
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
1313
* Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`.
14+
* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style.
1415
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`
1516

1617
# bayesplot 1.15.0

R/helpers-gg.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ modify_aes <- function(mapping, ...) {
3737
facet_wrap_parsed <- function(...) {
3838
facet_wrap(..., labeller = label_parsed)
3939
}
40-
dont_expand_y_axis <- function(expand = c(0,0)) {
40+
dont_expand_y_axis <- function(expand = expansion(0, 0)) {
4141
scale_y_continuous(expand = expand)
4242
}
43-
dont_expand_x_axis <- function(expand = c(0,0)) {
43+
dont_expand_x_axis <- function(expand = expansion(0, 0)) {
4444
scale_x_continuous(expand = expand)
4545
}
4646
dont_expand_axes <- function() {

R/mcmc-diagnostics-nuts.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ mcmc_nuts_acceptance <-
183183
)
184184
}
185185
hists <- hists +
186-
dont_expand_y_axis(c(0.005, 0)) +
186+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
187187
facet_wrap(vars(.data$Parameter), scales = "free") +
188188
yaxis_text(FALSE) +
189189
yaxis_title(FALSE) +
@@ -483,8 +483,8 @@ mcmc_nuts_energy <-
483483
) +
484484
scale_fill_manual("", values = fills, labels = aes_labs) +
485485
scale_color_manual("", values = clrs, labels = aes_labs) +
486-
dont_expand_y_axis(c(0.005, 0)) +
487-
scale_x_continuous(expand = c(0.2, 0)) +
486+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
487+
scale_x_continuous(expand = expansion(mult = 0.2, add = 0)) +
488488
labs(y = NULL, x = expression(E - bar(E))) +
489489
bayesplot_theme_get() +
490490
space_legend_keys() +

R/mcmc-diagnostics.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ mcmc_rhat <- function(rhat, ..., size = NULL) {
170170
labs(y = NULL, x = expression(hat(R))) +
171171
scale_fill_diagnostic("rhat") +
172172
scale_color_diagnostic("rhat") +
173-
scale_x_continuous(breaks = brks, expand = c(0, .01)) +
174-
scale_y_discrete(expand = c(.025,0)) +
173+
scale_x_continuous(breaks = brks, expand = expansion(mult = 0, add = 0.01)) +
174+
scale_y_discrete(expand = expansion(mult = 0.025, add = 0)) +
175175
yaxis_title(FALSE) +
176176
yaxis_text(FALSE) +
177177
yaxis_ticks(FALSE)
@@ -199,7 +199,7 @@ mcmc_rhat_hist <- function(rhat, ..., binwidth = NULL, bins = NULL, breaks = NUL
199199
scale_color_diagnostic("rhat") +
200200
scale_fill_diagnostic("rhat") +
201201
labs(x = expression(hat(R)), y = NULL) +
202-
dont_expand_y_axis(c(0.005, 0)) +
202+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
203203
bayesplot_theme_get() +
204204
yaxis_title(FALSE) +
205205
yaxis_text(FALSE) +
@@ -262,7 +262,7 @@ mcmc_neff <- function(ratio, ..., size = NULL) {
262262
# as.character truncates trailing zeroes, while ggplot default does not
263263
labels = as.character(breaks),
264264
limits = c(0, max(1, max_ratio) + 0.05),
265-
expand = c(0, 0)) +
265+
expand = expansion(0, 0)) +
266266
bayesplot_theme_get() +
267267
yaxis_text(FALSE) +
268268
yaxis_title(FALSE) +
@@ -290,7 +290,7 @@ mcmc_neff_hist <- function(ratio, ..., binwidth = NULL, bins = NULL, breaks = NU
290290
scale_color_diagnostic("neff_ratio") +
291291
scale_fill_diagnostic("neff_ratio") +
292292
labs(x = expression(N[eff]/N), y = NULL) +
293-
dont_expand_y_axis(c(0.005, 0)) +
293+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
294294
yaxis_title(FALSE) +
295295
yaxis_text(FALSE) +
296296
yaxis_ticks(FALSE) +
@@ -572,7 +572,7 @@ drop_NAs_and_warn <- function(x) {
572572
scale_x_continuous(
573573
limits = c(-0.5, lags + 0.5),
574574
breaks = function(x) as.integer(pretty(x, n = 3)),
575-
expand = c(0, 0)
575+
expand = expansion(0, 0)
576576
) +
577577
labs(x = "Lag", y = "Autocorrelation") +
578578
force_axes_in_facets()

R/mcmc-distributions.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ mcmc_dens_chains <- function(
332332
labs(color = "Chain") +
333333
scale_y_discrete(
334334
limits = unique(rev(data$parameter)),
335-
expand = c(0.05, .6)
335+
expand = expansion(mult = 0.05, add = 0.6)
336336
) +
337337
scale_color +
338338
bayesplot_theme_get() +
@@ -523,7 +523,7 @@ mcmc_dots_by_chain <- function(
523523
}
524524

525525
graph +
526-
dont_expand_y_axis(c(0.005, 0)) +
526+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
527527
bayesplot_theme_get() +
528528
yaxis_text(FALSE) +
529529
yaxis_title(FALSE) +
@@ -642,7 +642,7 @@ mcmc_dots_by_chain <- function(
642642
}
643643

644644
graph +
645-
dont_expand_y_axis(c(0.005, 0)) +
645+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
646646
bayesplot_theme_get() +
647647
yaxis_text(FALSE) +
648648
yaxis_ticks(FALSE) +
@@ -703,7 +703,7 @@ mcmc_dots_by_chain <- function(
703703
}
704704

705705
graph +
706-
dont_expand_y_axis(c(0.005, 0)) +
706+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
707707
bayesplot_theme_get() +
708708
yaxis_text(FALSE) +
709709
yaxis_title(FALSE) +

R/mcmc-parcoord.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ mcmc_parcoord <-
159159
}
160160

161161
graph +
162-
scale_x_discrete(expand = c(0,0), labels = levels(draws$Parameter)) +
162+
scale_x_discrete(expand = expansion(0, 0), labels = levels(draws$Parameter)) +
163163
expand_limits(x = nlevels(draws$Parameter) + 0.25) +
164164
labs(x = NULL, y = NULL)
165165
}

R/mcmc-traces.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ mcmc_rank_hist <- function(x,
446446
geom_blank(data = data_boundaries) +
447447
facet_call +
448448
force_x_axis_in_facets() +
449-
dont_expand_y_axis(c(0.005, 0)) +
449+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
450450
bayesplot_theme_get() +
451451
theme(
452452
axis.line.y = element_blank(),

R/ppc-test-statistics.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ ppc_stat_freqpoly <-
263263
values = set_names(get_color(c("m", "dh")), c("yrep", "y")),
264264
labels = c(yrep = Tyrep_label(), y = Ty_label())
265265
) +
266-
dont_expand_y_axis(c(0.005, 0)) +
266+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
267267
bayesplot_theme_get() +
268268
xaxis_title(FALSE) +
269269
yaxis_text(FALSE) +

R/ppd-test-statistics.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ ppd_stat_freqpoly <-
143143
name = stat_legend_title(stat, deparse(substitute(stat))),
144144
labels = Typred_label()
145145
) +
146-
dont_expand_y_axis(c(0.005, 0)) +
146+
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
147147
bayesplot_theme_get() +
148148
xaxis_title(FALSE) +
149149
yaxis_text(FALSE) +

tests/testthat/test-mcmc-diagnostics.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ test_that("'description' & 'rating' columns are correct (#176)", {
7979
expect_equal(df$description, expected_descriptions)
8080
})
8181

82+
test_that("mcmc_rhat uses the expected panel expansion", {
83+
rhats <- seq(from = 1, to = 1.20, length.out = 10)
84+
built <- ggplot2::ggplot_build(mcmc_rhat(rhats))
85+
86+
expect_equal(built$layout$panel_params[[1]]$x.range, c(0.99, 1.21))
87+
expect_equal(built$layout$panel_params[[1]]$y.range, c(0.775, 10.225))
88+
})
89+
8290
test_that("mcmc_acf & mcmc_acf_bar return a ggplot object", {
8391
expect_gg(mcmc_acf(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]"))
8492
expect_gg(mcmc_acf_bar(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]"))

0 commit comments

Comments
 (0)