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
35 changes: 20 additions & 15 deletions R/mcmc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,13 @@ mcmc_violin <- function(
...
) {

bw <- bw %||% "nrd0"
adjust <- adjust %||% 1
kernel <- kernel %||% "gaussian"
n_dens <- n_dens %||% 1024
bounds <- validate_density_bounds(bounds)
bw <- bw %||% "nrd0"
adjust <- adjust %||% 1
kernel <- kernel %||% "gaussian"
n_dens <- n_dens %||% 1024
bounds <- validate_density_bounds(bounds)

x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
x <- prepare_mcmc_array(x, pars, regex_pars, transformations)
data <- melt_mcmc.mcmc_array(x)
data$Chain <- factor(data$Chain)
n_param <- num_params(data)
Expand All @@ -486,16 +486,21 @@ mcmc_violin <- function(
geom_args <- list(linewidth = 0.5, na.rm = TRUE, alpha = alpha)
if (violin) {
geom_args[["draw_quantiles"]] <- probs
} else {
geom_args[["trim"]] <- trim
geom_args[["bw"]] <- bw
geom_args[["adjust"]] <- adjust
geom_args[["kernel"]] <- kernel
geom_args[["n"]] <- n_dens
if (!is.null(bounds)) {
geom_args[["bounds"]] <- bounds
}
if (utils::packageVersion("ggplot2") >= "4.0.0") {
geom_args[["draw_quantiles"]] <- NULL
geom_args[["quantiles"]] <- probs
geom_args[["quantile.linetype"]] <- 1
}
} else {
geom_args[["trim"]] <- trim
geom_args[["bw"]] <- bw
geom_args[["adjust"]] <- adjust
geom_args[["kernel"]] <- kernel
geom_args[["n"]] <- n_dens
if (!is.null(bounds)) {
geom_args[["bounds"]] <- bounds
}
}
if (by_chain) {
# aes_mapping[["color"]] <- ~ Chain
# aes_mapping[["group"]] <- ~ Chain
Expand Down
10 changes: 7 additions & 3 deletions R/ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,8 @@ ppc_dots <-

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

args_violin_y <- list(
data = function(x) dplyr::filter(x, .data$is_y),
Expand Down
5 changes: 2 additions & 3 deletions man/MCMC-distributions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions man/PPC-distributions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions tests/testthat/test-ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ test_that("density PPC/PPD plots reject invalid bounds", {
# non-numeric bounds
expect_error(ppc_dens_overlay(y, yrep, bounds = c("a", "b")),
"`bounds` must be a numeric vector of length 2")

# bounds with length != 2
expect_error(ppc_dens_overlay(y, yrep, bounds = c(0, 1, 2)),
"`bounds` must be a numeric vector of length 2")
expect_error(ppc_dens_overlay(y, yrep, bounds = 1),
"`bounds` must be a numeric vector of length 2")

# bounds with NA values
expect_error(ppc_dens_overlay(y, yrep, bounds = c(0, NA)),
"`bounds` must be a numeric vector of length 2")
expect_error(ppc_dens_overlay(y, yrep, bounds = c(NA, 1)),
"`bounds` must be a numeric vector of length 2")

# bounds where bounds[1] >= bounds[2]
expect_error(ppc_dens_overlay(y, yrep, bounds = c(1, 0)),
"`bounds` must satisfy bounds\\[1\\] < bounds\\[2\\]")
Expand Down Expand Up @@ -393,6 +393,17 @@ test_that("ppc_violin_grouped renders correctly", {
"ppc_violin_grouped (points, low jitter)",
p_dots_jitter)

p_probs <- ppc_violin_grouped(
y = vdiff_y,
yrep = vdiff_yrep,
group = vdiff_group,
y_draw = "points",
probs = c(0.1, 0.9)
)
vdiffr::expect_doppelganger(
"ppc_violin_grouped (points, probs)",
p_probs)

set.seed(seed = NULL)
})

Expand Down