Skip to content

Commit

Permalink
remove day_range argument from plot.epidist and use xlim instead and …
Browse files Browse the repository at this point in the history
…update doc using plot.epidist
  • Loading branch information
joshwlambert committed May 24, 2024
1 parent 499f70d commit c134a8b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
43 changes: 26 additions & 17 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
#' or probability density function (PDF) (in the case of continuous
#' distributions), or the cumulative distribution function (CDF).
#'
#' @param x An `<epidist>` object.
#' @param day_range Either `NULL` or a `numeric` vector of length 2 with the
#' first and last day to plot on the x-axis. If `NULL` the distribution is
#' @details
#' By default if the `xlim` argument is not specified the distribution is
#' plotted between day 0 and the 99th quantile of the distribution.
#' Alternatively, a `numeric` vector of length 2 with the
#' first and last day to plot on the x-axis can be supplied to `xlim`
#' (through [...]).
#'
#'
#' @param x An `<epidist>` object.
#' @param cumulative A boolean `logical`, default is `FALSE`.
#' `cumulative = TRUE` plots the cumulative distribution function (CDF).
#' @inheritParams base::print
Expand All @@ -27,7 +32,7 @@
#' plot(edist)
#'
#' # plot different day range (x-axis)
#' plot(edist, day_range = c(0, 10))
#' plot(edist, xlim = c(0, 10))
#'
#' # plot CDF
#' plot(edist, cumulative = TRUE)
Expand All @@ -36,14 +41,15 @@
#' edist <- discretise(edist)
#' plot(edist)
plot.epidist <- function(x,
day_range = NULL,
cumulative = FALSE,
...) {
# check input
validate_epidist(x)
checkmate::assert_numeric(day_range, len = 2, null.ok = TRUE)
checkmate::assert_logical(cumulative, any.missing = FALSE, len = 1)

# capture dots
dots <- list(...)

oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))

Expand All @@ -53,10 +59,11 @@ plot.epidist <- function(x,
main <- "Probability Mass Function"
}

if (is.null(day_range)) {
day_range <- seq(0, quantile(x, p = 0.99), length.out = 1000)
if (is.null(dots$xlim)) {
xlim <- seq(0, quantile(x, p = 0.99), length.out = 1000)
} else {
day_range <- seq(day_range[1], day_range[2], length.out = 1000)
checkmate::assert_numeric(dots$xlim, len = 2)
xlim <- seq(dots$xlim[1], dots$xlim[2], length.out = 1000)
}

xlab <- tools::toTitleCase(x$epi_dist)
Expand All @@ -68,8 +75,8 @@ plot.epidist <- function(x,
if (cumulative) {
# plot CDF
plot(
day_range,
cdf(x, q = day_range),
x = xlim,
y = cdf(x, q = xlim),
ylab = "",
xlab = xlab,
type = "l",
Expand All @@ -82,8 +89,8 @@ plot.epidist <- function(x,
} else {
# plot either PDF or PMF
plot(
day_range,
density(x, at = day_range),
x = xlim,
y = density(x, at = xlim),
ylab = "",
xlab = xlab,
type = "l",
Expand All @@ -96,17 +103,19 @@ plot.epidist <- function(x,
} else {
if (cumulative) {
graphics::barplot(
cdf(x, q = unique(round(day_range))),
names.arg = unique(round(day_range)),
height = cdf(x, q = unique(round(xlim))),
space = 0.2,
names.arg = unique(round(xlim)),
xlab = xlab,
ylab = "",
main = "Cumulative Distribution Function",
...
)
} else {
graphics::barplot(
density(x, at = unique(round(day_range))),
names.arg = unique(round(day_range)),
density(x, at = unique(round(xlim))),
space = 0.2,
names.arg = unique(round(xlim)),
xlab = xlab,
ylab = "",
main = main,
Expand Down
1 change: 1 addition & 0 deletions man/epiparameter-package.Rd

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

15 changes: 9 additions & 6 deletions man/plot.epidist.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-epidist.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ test_that("epidist.plot does not produce an error", {
)
})

test_that("epidist.plot works with non-default day_range", {
test_that("epidist.plot works with non-default x-axis", {
ebola_dist <- suppressMessages(epidist(
disease = "ebola",
epi_dist = "incubation",
Expand All @@ -205,14 +205,14 @@ test_that("epidist.plot works with non-default day_range", {
expect_silent(
plot(
ebola_dist,
day_range = c(0, 20)
xlim = c(0, 20)
)
)

f <- function() {
plot(
ebola_dist,
day_range = c(0, 20)
xlim = c(0, 20)
)
}
vdiffr::expect_doppelganger(
Expand Down
4 changes: 2 additions & 2 deletions vignettes/epiparameter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ generate(ebola_incubation, times = 10)
plot(ebola_incubation)
```

The default plotting range for time since infection is from zero to the 99th quantile of the distribution. This can be altered by specifying the `day_range` argument when plotting an `<epidist>` object.
The default plotting range for time since infection is from zero to the 99th quantile of the distribution. This can be altered by specifying the `xlim` argument when plotting an `<epidist>` object.

```{r plot-epidist-dayrange}
plot(ebola_incubation, day_range = c(1, 25))
plot(ebola_incubation, xlim = c(1, 25))
```

This plotting function can be useful for visually comparing epidemiological distributions from different publications on the same disease. In addition, plotting the distribution after manually creating an `<epidist>` help to check that the parameters are sensible and produce the expected distribution.
Expand Down

0 comments on commit c134a8b

Please sign in to comment.