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 5 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 DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Imports:
tidyselect,
utils
Suggests:
ggdist,
ggfortify,
gridExtra (>= 2.2.1),
hexbin,
Expand Down
51 changes: 49 additions & 2 deletions R/ppc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' @template args-pit-ecdf
#' @param size,alpha Passed to the appropriate geom to control the appearance of
#' the predictive distributions.
#' @param ... Currently unused.
#' @param ... For dot plots, optional additional arguments to pass to [ggdist::stat_dots()].
#'
#' @template details-binomial
#' @template return-ggplot-or-data
Expand All @@ -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(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))

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,
...
) +
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(..., ok_args = c("dotsize", "layout", "stackratio", "overflow"))

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,
...
) +
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
4 changes: 4 additions & 0 deletions man-roxygen/args-qdotplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#' @param quantiles For dot plots, an optional integer passed to
#' [ggdist::stat_dots()] specifying the number of quantiles to use for a
#' quantile dot plot. If `quantiles` is `NA` (the default) then all data
#' points are plotted.
853 changes: 853 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-binwidth.new.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,136 changes: 1,136 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-default.new.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
704 changes: 704 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppc-qdotplot-quantile.new.svg
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.
720 changes: 720 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-binwidth.new.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,025 changes: 1,025 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-default.new.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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
643 changes: 643 additions & 0 deletions tests/testthat/_snaps/ppc-distributions/ppd-qdotplot-quantile.new.svg
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.
52 changes: 52 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(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,42 @@ 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)
expect_warning(vdiffr::expect_doppelganger("ppc_qdotplot (binwidth)", p_binwidth),
"The provided binwidth will cause dots to overflow the boundaries of the geometry.")

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)
expect_warning(vdiffr::expect_doppelganger("ppc_qdotplot (quantile-binwidth)", p_quantile_binwidth),
"The provided binwidth will cause dots to overflow the boundaries of the geometry.")

# 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)
expect_warning(vdiffr::expect_doppelganger("ppd_qdotplot (binwidth)", p_binwidth),
"The provided binwidth will cause dots to overflow the boundaries of the geometry.")

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)
expect_warning(vdiffr::expect_doppelganger("ppd_qdotplot (quantile-binwidth)", p_quantile_binwidth),
"The provided binwidth will cause dots to overflow the boundaries of the geometry.")
})

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