From 260804e3a8ac62e6d86c783ccf4409261a3ba57e Mon Sep 17 00:00:00 2001 From: "Watal M. Iwasaki" Date: Sun, 4 Jun 2023 22:48:24 +0900 Subject: [PATCH] Follow ggplot2 updates on facet_grid() and facet_wrap() - Replace facets with rows/cols for facet_grid() - Replace formula ~parameter with vars(parameter) Addresses #304 --- R/mcmc-diagnostics-nuts.R | 6 +++--- R/mcmc-diagnostics.R | 3 ++- R/mcmc-distributions.R | 11 +++++------ R/mcmc-traces.R | 9 +++++---- R/ppc-errors.R | 5 +++-- man/MCMC-nuts.Rd | 2 +- tests/testthat/test-ppc-distributions.R | 2 +- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/R/mcmc-diagnostics-nuts.R b/R/mcmc-diagnostics-nuts.R index 1cfe69e6..30f565f2 100644 --- a/R/mcmc-diagnostics-nuts.R +++ b/R/mcmc-diagnostics-nuts.R @@ -117,7 +117,7 @@ #' mcmc_nuts_energy(np) #' mcmc_nuts_energy(np, merge_chains = TRUE, binwidth = .15) #' mcmc_nuts_energy(np) + -#' facet_wrap(~ Chain, nrow = 1) + +#' facet_wrap(vars(Chain), nrow = 1) + #' coord_fixed(ratio = 150) + #' ggtitle("NUTS Energy Diagnostic") #' } @@ -180,7 +180,7 @@ mcmc_nuts_acceptance <- } hists <- hists + dont_expand_y_axis(c(0.005, 0)) + - facet_wrap(~ Parameter, scales = "free") + + facet_wrap(vars(Parameter), scales = "free") + yaxis_text(FALSE) + yaxis_title(FALSE) + yaxis_ticks(FALSE) + @@ -476,7 +476,7 @@ mcmc_nuts_energy <- } graph + - facet_wrap(~ Chain) + + facet_wrap(vars(Chain)) + force_axes_in_facets() } diff --git a/R/mcmc-diagnostics.R b/R/mcmc-diagnostics.R index 2751872a..02ac547a 100644 --- a/R/mcmc-diagnostics.R +++ b/R/mcmc-diagnostics.R @@ -519,7 +519,8 @@ drop_NAs_and_warn <- function(x) { plot_data <- acf_data(x = x, lags = lags) if (num_chains(x) > 1) { - facet_args$facets <- "Chain ~ Parameter" + facet_args$rows <- vars(Chain) + facet_args$cols <- vars(Parameter) facet_fun <- "facet_grid" } else { # 1 chain facet_args$facets <- "Parameter" diff --git a/R/mcmc-distributions.R b/R/mcmc-distributions.R index c975273a..0576accb 100644 --- a/R/mcmc-distributions.R +++ b/R/mcmc-distributions.R @@ -405,14 +405,13 @@ mcmc_violin <- function( facet_args[["scales"]] <- facet_args[["scales"]] %||% "free" if (!by_chain) { if (n_param > 1) { - facet_args[["facets"]] <- ~ Parameter + facet_args[["facets"]] <- vars(Parameter) graph <- graph + do.call("facet_wrap", facet_args) } } else { - facet_args[["facets"]] <- if (n_param > 1) { - "Chain ~ Parameter" - } else { - "Chain ~ ." + facet_args[["rows"]] <- vars(Chain) + if (n_param > 1) { + facet_args[["cols"]] <- vars(Parameter) } graph <- graph + do.call("facet_grid", facet_args) + @@ -527,7 +526,7 @@ mcmc_violin <- function( labs(x = if (violin) "Chain" else levels(data$Parameter), y = if (violin) levels(data$Parameter) else NULL) } else { - facet_args[["facets"]] <- ~ Parameter + facet_args[["facets"]] <- vars(Parameter) facet_args[["scales"]] <- facet_args[["scales"]] %||% "free" graph <- graph + do.call("facet_wrap", facet_args) } diff --git a/R/mcmc-traces.R b/R/mcmc-traces.R index ed92060d..3eba70de 100644 --- a/R/mcmc-traces.R +++ b/R/mcmc-traces.R @@ -335,7 +335,7 @@ mcmc_rank_overlay <- function(x, facet_call <- NULL if (n_param > 1) { - facet_args$facets <- ~ parameter + facet_args$facets <- vars(parameter) facet_args$scales <- facet_args$scales %||% "fixed" facet_call <- do.call("facet_wrap", facet_args) } @@ -387,7 +387,8 @@ mcmc_rank_hist <- function(x, right_edge <- max(data_boundaries$value_rank) facet_args[["scales"]] <- facet_args[["scales"]] %||% "fixed" - facet_args[["facets"]] <- facet_args[["facets"]] %||% (parameter ~ chain) + facet_args[["rows"]] <- facet_args[["rows"]] %||% vars(parameter) + facet_args[["cols"]] <- facet_args[["cols"]] %||% vars(chain) # If there is one parameter, put the chains in one row. # Otherwise, use a grid. @@ -526,7 +527,7 @@ mcmc_rank_ecdf <- if (n_param == 1) { facet_call <- ylab(levels(data$parameter)) } else { - facet_args$facets <- ~parameter + facet_args$facets <- vars(parameter) facet_args$scales <- facet_args$scales %||% "free" facet_call <- do.call("facet_wrap", facet_args) } @@ -705,7 +706,7 @@ mcmc_trace_data <- function(x, if (n_param == 1) { facet_call <- ylab(levels(data$parameter)) } else { - facet_args$facets <- ~ parameter + facet_args$facets <- vars(parameter) facet_args$scales <- facet_args$scales %||% "free" facet_call <- do.call("facet_wrap", facet_args) } diff --git a/R/ppc-errors.R b/R/ppc-errors.R index 100a9cb1..5902b441 100644 --- a/R/ppc-errors.R +++ b/R/ppc-errors.R @@ -398,10 +398,11 @@ error_hist_facets <- if (grouped) { facet_fun <- "facet_grid" - facet_args[["facets"]] <- rep_id ~ group + facet_args[["rows"]] <- vars(rep_id) + facet_args[["cols"]] <- vars(group) } else { facet_fun <- "facet_wrap" - facet_args[["facets"]] <- ~ rep_id + facet_args[["facets"]] <- vars(rep_id) } facet_args[["scales"]] <- facet_args[["scales"]] %||% scales_default diff --git a/man/MCMC-nuts.Rd b/man/MCMC-nuts.Rd index 72feacbc..c0e27f8a 100644 --- a/man/MCMC-nuts.Rd +++ b/man/MCMC-nuts.Rd @@ -145,7 +145,7 @@ color_scheme_set("red") mcmc_nuts_energy(np) mcmc_nuts_energy(np, merge_chains = TRUE, binwidth = .15) mcmc_nuts_energy(np) + - facet_wrap(~ Chain, nrow = 1) + + facet_wrap(vars(Chain), nrow = 1) + coord_fixed(ratio = 150) + ggtitle("NUTS Energy Diagnostic") } diff --git a/tests/testthat/test-ppc-distributions.R b/tests/testthat/test-ppc-distributions.R index 1aa017d7..c89eb28b 100644 --- a/tests/testthat/test-ppc-distributions.R +++ b/tests/testthat/test-ppc-distributions.R @@ -38,7 +38,7 @@ test_that("ppc_dens,pp_hist,ppc_freqpoly,ppc_boxplot return ggplot objects", { expect_gg(p <- ppc_hist(y, yrep[1:8, ], binwidth = 3)) if (utils::packageVersion("ggplot2") >= "3.0.0") { - facet_var <- "~rep_label" + facet_var <- vars(rep_label) expect_equal(as.character(p$facet$params$facets[1]), facet_var) }