Skip to content

add quantile dot plot functions #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Suggests:
shinystan (>= 2.3.0),
survival,
testthat (>= 2.0.0),
vdiffr (>= 1.0.2)
vdiffr (>= 1.0.2),
ggdist
RoxygenNote: 7.3.2
VignetteBuilder: knitr
Encoding: UTF-8
Expand Down
49 changes: 48 additions & 1 deletion R/ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#' dataset (row) in `yrep`. For these plots `yrep` should therefore
#' contain only a small number of rows. See the **Examples** section.
#' }
#' \item{`ppc_qdotplot()`}{
#' A dot plot plot is displayed for `y` and each dataset (row) in `yrep`.
#' For these plots `yrep` should therefore contain only a small number of rows.
#' See the **Examples** section. This function requires [ggdist::stat_dots] to be installed.
#' }
#' \item{`ppc_freqpoly_grouped()`}{
#' A separate frequency polygon is plotted for each level of a grouping
#' variable for `y` and each dataset (row) in `yrep`. For this plot
Expand Down Expand Up @@ -80,7 +85,7 @@
#' ppc_pit_ecdf(y, yrep, prob = 0.99, plot_diff = TRUE)
#' }
#'
#' # for ppc_hist,dens,freqpoly,boxplot definitely use a subset yrep rows so
#' # for ppc_hist,dens,freqpoly,boxplot,qdotplot definitely use a subset yrep rows so
#' # only a few (instead of nrow(yrep)) histograms are plotted
#' ppc_hist(y, yrep[1:8, ])
#' \donttest{
Expand All @@ -90,6 +95,9 @@
#' # wizard hat plot
#' color_scheme_set("blue")
#' ppc_dens(y, yrep[200:202, ])
#'
#' # dot plot
#' ppc_qdotplot(y, yrep[1:8, ])
#' }
#'
#' \donttest{
Expand Down Expand Up @@ -507,6 +515,45 @@ ppc_boxplot <-
xaxis_title(FALSE)
}

#' @rdname PPC-distributions
#' @export
#' @template args-qdotplot
ppc_qdotplot <-
function(y,
yrep,
...,
binwidth = NA,
quantiles = NA,
freq = TRUE) {
check_ignored_arguments(...)

suggested_package("ggdist")

data <- ppc_data(y, yrep)

ggplot(data, mapping = set_hist_aes(
freq = freq,
fill = .data$is_y_label,
color = .data$is_y_label
)) +
ggdist::stat_dots(
binwidth = binwidth,
quantiles = quantiles,
overflow = "warn"
) +
scale_fill_ppc() +
scale_color_ppc() +
facet_wrap_parsed("rep_label") +
force_axes_in_facets() +
bayesplot_theme_get() +
space_legend_keys() +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
yaxis_ticks(FALSE) +
xaxis_title(FALSE) +
facet_text(FALSE) +
facet_bg(FALSE)
}

#' @rdname PPC-distributions
#' @export
Expand Down
37 changes: 37 additions & 0 deletions R/ppd-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@ ppd_hist <-
facet_text(FALSE)
}

#' @rdname PPD-distributions
#' @export
ppd_qdotplot <-
function(ypred,
...,
binwidth = NA,
quantiles = NA,
freq = TRUE) {
check_ignored_arguments(...)

suggested_package("ggdist")

data <- ppd_data(ypred)
ggplot(data, mapping = set_hist_aes(
freq,
color = "ypred",
fill = "ypred"
)) +
ggdist::stat_dots(
binwidth = binwidth,
quantiles = quantiles,
overflow = "warn"
) +
scale_color_ppd() +
scale_fill_ppd() +
bayesplot_theme_get() +
facet_wrap_parsed("rep_label") +
force_axes_in_facets() +
dont_expand_y_axis() +
legend_none() +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
yaxis_ticks(FALSE) +
xaxis_title(FALSE) +
facet_text(FALSE)
}


#' @rdname PPD-distributions
#' @export
Expand Down
3 changes: 3 additions & 0 deletions man-roxygen/args-qdotplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#' @param quantiles For dot plots, optional integer `quantiles` defines the
#' number of quantiles to plot in dot plot to produce a quantile dot plot.
#' If `quantiles` is `NA` (the default) then all data points are plotted.
950 changes: 950 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-binwidth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,265 changes: 1,265 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
869 changes: 869 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-quantile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
809 changes: 809 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-binwidth.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,138 changes: 1,138 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
787 changes: 787 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-quantile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions tests/testthat/test-ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ test_that("ppc_dens,pp_hist,ppc_freqpoly,ppc_boxplot return ggplot objects", {
expect_gg(ppd_boxplot(yrep2, notch = FALSE))
})

test_that("ppc_qdotplot returns a ggplot object", {
testthat::skip_if_not_installed("ggdist")

expect_gg(ppc_qdotplot(y, yrep[1:8, ]))
expect_gg(ppc_qdotplot(y, yrep[1,, drop = FALSE], quantiles=25))
expect_gg(ppc_qdotplot(y, yrep[1:8, ], binwidth = 0.1))
expect_gg(ppc_qdotplot(y2, yrep2, binwidth = 0.1, quantiles = 25))


# ppd versions
expect_gg(ppd_qdotplot(y, yrep[1:8, ]))
expect_gg(ppd_qdotplot(yrep[1,, drop = FALSE], quantiles = 25))
expect_gg(ppd_qdotplot(yrep[1:8, ], binwidth = 0.1))
expect_gg(ppd_qdotplot(yrep2, binwidth = 0.1, quantiles = 25))
})

test_that("ppc_pit_ecdf, ppc_pit_ecdf_grouped returns a ggplot object", {
expect_gg(ppc_pit_ecdf(y, yrep, interpolate_adj = FALSE))
expect_gg(ppc_pit_ecdf_grouped(y, yrep, group = group, interpolate_adj = FALSE))
Expand Down Expand Up @@ -177,6 +193,38 @@ test_that("ppc_boxplot renders correctly", {
vdiffr::expect_doppelganger("ppd_boxplot (alpha, size)", p_custom)
})

test_that("ppc_qdotplot renders correctly", {
testthat::skip_on_cran()
testthat::skip_if_not_installed("vdiffr")
testthat::skip_if_not_installed("ggdist")
skip_on_r_oldrel()

p_base <- ppc_qdotplot(vdiff_y, vdiff_yrep[1:8, ])
vdiffr::expect_doppelganger("ppc_qdotplot (default)", p_base)

p_binwidth <- ppc_qdotplot(vdiff_y, vdiff_yrep[1:8, ], binwidth = 3)
vdiffr::expect_doppelganger("ppc_qdotplot (binwidth)", p_binwidth)

p_quantile <- ppc_qdotplot(vdiff_y, vdiff_yrep[1:8, ], quantiles = 50)
vdiffr::expect_doppelganger("ppc_qdotplot (quantile)", p_quantile)

p_quantile_binwidth <- ppc_qdotplot(vdiff_y, vdiff_yrep[1:8, ], binwidth = 3, quantiles = 50)
vdiffr::expect_doppelganger("ppc_qdotplot (quantile-binwidth)", p_quantile_binwidth)

# ppd versions
p_base <- ppd_qdotplot(vdiff_yrep[1:8, ])
vdiffr::expect_doppelganger("ppd_qdotplot (default)", p_base)

p_binwidth <- ppd_qdotplot(vdiff_yrep[1:8, ], binwidth = 3)
vdiffr::expect_doppelganger("ppd_qdotplot (binwidth)", p_binwidth)

p_quantile <- ppd_qdotplot(vdiff_yrep[1:8, ], quantiles = 50)
vdiffr::expect_doppelganger("ppd_qdotplot (quantile)", p_quantile)

p_quantile_binwidth <- ppd_qdotplot(vdiff_yrep[1:8, ], binwidth = 3, quantiles = 50)
vdiffr::expect_doppelganger("ppd_qdotplot (quantile-binwidth)", p_quantile_binwidth)
})

test_that("ppc_ecdf_overlay renders correctly", {
testthat::skip_on_cran()
testthat::skip_if_not_installed("vdiffr")
Expand Down
Loading