Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* New functions `mcmc_dots` and `mcmc_dots_by_chain` for dot plots of MCMC draws by @behramulukir (#402)
* Default to `quantiles=100` for all dot plots by @behramulukir (#402)
* Use `"neff_ratio"` consistently in diagnostic color scale helpers to avoid relying on partial matching of `"neff"`.
* Replace `expand = c(mult, add)` with `ggplot2::expansion()` helper in scale functions for consistency with ggplot2 >= 3.3.0 style.
* Replace uses of `geom_bar(stat = "identity")` with the more idiomatic ggplot2 form `geom_col()`

# bayesplot 1.15.0
Expand Down
4 changes: 2 additions & 2 deletions R/helpers-gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ modify_aes <- function(mapping, ...) {
facet_wrap_parsed <- function(...) {
facet_wrap(..., labeller = label_parsed)
}
dont_expand_y_axis <- function(expand = c(0,0)) {
dont_expand_y_axis <- function(expand = expansion(0, 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the default here is good, but there are still places in the code where we call e.g.

dont_expand_y_axis(c(0.005, 0))

in which case this is still using the old behavior.

scale_y_continuous(expand = expand)
}
dont_expand_x_axis <- function(expand = c(0,0)) {
dont_expand_x_axis <- function(expand = expansion(0, 0)) {
scale_x_continuous(expand = expand)
}
dont_expand_axes <- function() {
Expand Down
6 changes: 3 additions & 3 deletions R/mcmc-diagnostics-nuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mcmc_nuts_acceptance <-
)
}
hists <- hists +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
facet_wrap(vars(.data$Parameter), scales = "free") +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
Expand Down Expand Up @@ -483,8 +483,8 @@ mcmc_nuts_energy <-
) +
scale_fill_manual("", values = fills, labels = aes_labs) +
scale_color_manual("", values = clrs, labels = aes_labs) +
dont_expand_y_axis(c(0.005, 0)) +
scale_x_continuous(expand = c(0.2, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
scale_x_continuous(expand = expansion(mult = 0.2, add = 0)) +
labs(y = NULL, x = expression(E - bar(E))) +
bayesplot_theme_get() +
space_legend_keys() +
Expand Down
12 changes: 6 additions & 6 deletions R/mcmc-diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ mcmc_rhat <- function(rhat, ..., size = NULL) {
labs(y = NULL, x = expression(hat(R))) +
scale_fill_diagnostic("rhat") +
scale_color_diagnostic("rhat") +
scale_x_continuous(breaks = brks, expand = c(0, .01)) +
scale_y_discrete(expand = c(.025,0)) +
scale_x_continuous(breaks = brks, expand = expansion(mult = 0, add = 0.01)) +
scale_y_discrete(expand = expansion(mult = 0.025, add = 0)) +
yaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE)
Expand Down Expand Up @@ -199,7 +199,7 @@ mcmc_rhat_hist <- function(rhat, ..., binwidth = NULL, bins = NULL, breaks = NUL
scale_color_diagnostic("rhat") +
scale_fill_diagnostic("rhat") +
labs(x = expression(hat(R)), y = NULL) +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
yaxis_title(FALSE) +
yaxis_text(FALSE) +
Expand Down Expand Up @@ -262,7 +262,7 @@ mcmc_neff <- function(ratio, ..., size = NULL) {
# as.character truncates trailing zeroes, while ggplot default does not
labels = as.character(breaks),
limits = c(0, max(1, max_ratio) + 0.05),
expand = c(0, 0)) +
expand = expansion(0, 0)) +
bayesplot_theme_get() +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
Expand Down Expand Up @@ -290,7 +290,7 @@ mcmc_neff_hist <- function(ratio, ..., binwidth = NULL, bins = NULL, breaks = NU
scale_color_diagnostic("neff_ratio") +
scale_fill_diagnostic("neff_ratio") +
labs(x = expression(N[eff]/N), y = NULL) +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
yaxis_title(FALSE) +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
Expand Down Expand Up @@ -572,7 +572,7 @@ drop_NAs_and_warn <- function(x) {
scale_x_continuous(
limits = c(-0.5, lags + 0.5),
breaks = function(x) as.integer(pretty(x, n = 3)),
expand = c(0, 0)
expand = expansion(0, 0)
) +
labs(x = "Lag", y = "Autocorrelation") +
force_axes_in_facets()
Expand Down
8 changes: 4 additions & 4 deletions R/mcmc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ mcmc_dens_chains <- function(
labs(color = "Chain") +
scale_y_discrete(
limits = unique(rev(data$parameter)),
expand = c(0.05, .6)
expand = expansion(mult = 0.05, add = 0.6)
) +
scale_color +
bayesplot_theme_get() +
Expand Down Expand Up @@ -523,7 +523,7 @@ mcmc_dots_by_chain <- function(
}

graph +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
Expand Down Expand Up @@ -642,7 +642,7 @@ mcmc_dots_by_chain <- function(
}

graph +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
Expand Down Expand Up @@ -703,7 +703,7 @@ mcmc_dots_by_chain <- function(
}

graph +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
Expand Down
2 changes: 1 addition & 1 deletion R/mcmc-parcoord.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mcmc_parcoord <-
}

graph +
scale_x_discrete(expand = c(0,0), labels = levels(draws$Parameter)) +
scale_x_discrete(expand = expansion(0, 0), labels = levels(draws$Parameter)) +
expand_limits(x = nlevels(draws$Parameter) + 0.25) +
labs(x = NULL, y = NULL)
}
Expand Down
2 changes: 1 addition & 1 deletion R/mcmc-traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ mcmc_rank_hist <- function(x,
geom_blank(data = data_boundaries) +
facet_call +
force_x_axis_in_facets() +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
theme(
axis.line.y = element_blank(),
Expand Down
2 changes: 1 addition & 1 deletion R/ppc-test-statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ ppc_stat_freqpoly <-
values = set_names(get_color(c("m", "dh")), c("yrep", "y")),
labels = c(yrep = Tyrep_label(), y = Ty_label())
) +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
Expand Down
2 changes: 1 addition & 1 deletion R/ppd-test-statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ ppd_stat_freqpoly <-
name = stat_legend_title(stat, deparse(substitute(stat))),
labels = Typred_label()
) +
dont_expand_y_axis(c(0.005, 0)) +
dont_expand_y_axis(expansion(mult = 0.005, add = 0)) +
bayesplot_theme_get() +
xaxis_title(FALSE) +
yaxis_text(FALSE) +
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-mcmc-diagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ test_that("'description' & 'rating' columns are correct (#176)", {
expect_equal(df$description, expected_descriptions)
})

test_that("mcmc_rhat uses the expected panel expansion", {
rhats <- seq(from = 1, to = 1.20, length.out = 10)
built <- ggplot2::ggplot_build(mcmc_rhat(rhats))

expect_equal(built$layout$panel_params[[1]]$x.range, c(0.99, 1.21))
expect_equal(built$layout$panel_params[[1]]$y.range, c(0.775, 10.225))
})

test_that("mcmc_acf & mcmc_acf_bar return a ggplot object", {
expect_gg(mcmc_acf(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]"))
expect_gg(mcmc_acf_bar(arr, pars = "beta[1]", regex_pars = "x\\:[2,5]"))
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-mcmc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ test_that("mcmc_dots_by_chain returns a ggplot object", {
quantiles = 80))
})

test_that("mcmc_dots_by_chain uses the expected y-axis expansion", {
built <- ggplot2::ggplot_build(
mcmc_dots_by_chain(
example_mcmc_draws(),
pars = c("beta[1]", "beta[2]"),
quantiles = 50
)
)

expect_equal(built$layout$panel_params[[1]]$y.range, c(-0.005, 1.005))
})

test_that("mcmc_violin returns a ggplot object", {
expect_gg(mcmc_violin(arr, pars = "beta[2]", regex_pars = "x\\:"))
expect_gg(mcmc_violin(dframe_multiple_chains,
Expand Down Expand Up @@ -294,4 +306,3 @@ test_that("mcmc_dots_by_chain renders correctly", {
p_base <- mcmc_dots_by_chain(vdiff_dframe_chains)
vdiffr::expect_doppelganger("mcmc_dots_by_chain (default)", p_base)
})

8 changes: 8 additions & 0 deletions tests/testthat/test-mcmc-scatter-and-parcoord.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ test_that("mcmc_parcoord returns a ggplot object", {
expect_gg(mcmc_parcoord(arr, pars = "sigma", regex_pars = "beta"))
})

test_that("mcmc_parcoord uses the expected x-axis expansion", {
built <- ggplot2::ggplot_build(
mcmc_parcoord(example_mcmc_draws(), pars = c("beta[1]", "beta[2]", "sigma"))
)

expect_equal(built$layout$panel_params[[1]]$x.range, c(1, 3.25))
})

test_that("mcmc_parcoord with nuts info returns a ggplot object", {
skip_if_not_installed("rstanarm")
expect_gg(mcmc_parcoord(post, pars = c("wt", "am", "sigma"), np = np))
Expand Down
Loading