Skip to content

Commit aa1a309

Browse files
committed
Fix violin plot deprecation warnings from ggplot2 4.0
1 parent edffd04 commit aa1a309

6 files changed

Lines changed: 214 additions & 27 deletions

File tree

R/mcmc-distributions.R

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ mcmc_violin <- function(
460460
...
461461
) {
462462

463-
bw <- bw %||% "nrd0"
464-
adjust <- adjust %||% 1
465-
kernel <- kernel %||% "gaussian"
466-
n_dens <- n_dens %||% 1024
467-
bounds <- validate_density_bounds(bounds)
463+
bw <- bw %||% "nrd0"
464+
adjust <- adjust %||% 1
465+
kernel <- kernel %||% "gaussian"
466+
n_dens <- n_dens %||% 1024
467+
bounds <- validate_density_bounds(bounds)
468468

469-
x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
469+
x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
470470
data <- melt_mcmc.mcmc_array(x)
471471
data$Chain <- factor(data$Chain)
472472
n_param <- num_params(data)
@@ -486,16 +486,21 @@ mcmc_violin <- function(
486486
geom_args <- list(linewidth = 0.5, na.rm = TRUE, alpha = alpha)
487487
if (violin) {
488488
geom_args[["draw_quantiles"]] <- probs
489-
} else {
490-
geom_args[["trim"]] <- trim
491-
geom_args[["bw"]] <- bw
492-
geom_args[["adjust"]] <- adjust
493-
geom_args[["kernel"]] <- kernel
494-
geom_args[["n"]] <- n_dens
495-
if (!is.null(bounds)) {
496-
geom_args[["bounds"]] <- bounds
497-
}
489+
if (utils::packageVersion("ggplot2") >= "4.0.0") {
490+
geom_args[["draw_quantiles"]] <- NULL
491+
geom_args[["quantiles"]] <- probs
492+
geom_args[["quantile.linetype"]] <- 1
498493
}
494+
} else {
495+
geom_args[["trim"]] <- trim
496+
geom_args[["bw"]] <- bw
497+
geom_args[["adjust"]] <- adjust
498+
geom_args[["kernel"]] <- kernel
499+
geom_args[["n"]] <- n_dens
500+
if (!is.null(bounds)) {
501+
geom_args[["bounds"]] <- bounds
502+
}
503+
}
499504
if (by_chain) {
500505
# aes_mapping[["color"]] <- ~ Chain
501506
# aes_mapping[["group"]] <- ~ Chain

R/ppc-distributions.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,8 @@ ppc_dots <-
566566

567567
#' @rdname PPC-distributions
568568
#' @export
569-
#' @param probs A numeric vector passed to [ggplot2::geom_violin()]'s
570-
#' `draw_quantiles` argument to specify at which quantiles to draw
571-
#' horizontal lines. Set to `NULL` to remove the lines.
569+
#' @param probs A numeric vector of probabilities controlling where quantile
570+
#' lines are drawn. Set to `NULL` to remove the lines.
572571
#' @param y_draw For `ppc_violin_grouped()`, a string specifying how to draw
573572
#' `y`: `"violin"` (default), `"points"` (jittered points), or `"both"`.
574573
#' @param y_jitter,y_size,y_alpha For `ppc_violin_grouped()`, if `y_draw` is
@@ -602,6 +601,11 @@ ppc_violin_grouped <-
602601
alpha = alpha,
603602
linewidth = size
604603
)
604+
if (utils::packageVersion("ggplot2") >= "4.0.0") {
605+
args_violin_yrep$draw_quantiles <- NULL
606+
args_violin_yrep$quantiles <- probs
607+
args_violin_yrep$quantile.linetype <- 1
608+
}
605609

606610
args_violin_y <- list(
607611
data = function(x) dplyr::filter(x, .data$is_y),

man/MCMC-distributions.Rd

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

man/PPC-distributions.Rd

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

tests/testthat/_snaps/ppc-distributions/ppc-violin-grouped-points-probs.svg

Lines changed: 169 additions & 0 deletions
Loading

tests/testthat/test-ppc-distributions.R

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ test_that("density PPC/PPD plots reject invalid bounds", {
2121
# non-numeric bounds
2222
expect_error(ppc_dens_overlay(y, yrep, bounds = c("a", "b")),
2323
"`bounds` must be a numeric vector of length 2")
24-
24+
2525
# bounds with length != 2
2626
expect_error(ppc_dens_overlay(y, yrep, bounds = c(0, 1, 2)),
2727
"`bounds` must be a numeric vector of length 2")
2828
expect_error(ppc_dens_overlay(y, yrep, bounds = 1),
2929
"`bounds` must be a numeric vector of length 2")
30-
30+
3131
# bounds with NA values
3232
expect_error(ppc_dens_overlay(y, yrep, bounds = c(0, NA)),
3333
"`bounds` must be a numeric vector of length 2")
3434
expect_error(ppc_dens_overlay(y, yrep, bounds = c(NA, 1)),
3535
"`bounds` must be a numeric vector of length 2")
36-
36+
3737
# bounds where bounds[1] >= bounds[2]
3838
expect_error(ppc_dens_overlay(y, yrep, bounds = c(1, 0)),
3939
"`bounds` must satisfy bounds\\[1\\] < bounds\\[2\\]")
@@ -393,6 +393,17 @@ test_that("ppc_violin_grouped renders correctly", {
393393
"ppc_violin_grouped (points, low jitter)",
394394
p_dots_jitter)
395395

396+
p_probs <- ppc_violin_grouped(
397+
y = vdiff_y,
398+
yrep = vdiff_yrep,
399+
group = vdiff_group,
400+
y_draw = "points",
401+
probs = c(0.1, 0.9)
402+
)
403+
vdiffr::expect_doppelganger(
404+
"ppc_violin_grouped (points, probs)",
405+
p_probs)
406+
396407
set.seed(seed = NULL)
397408
})
398409

0 commit comments

Comments
 (0)