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
33 changes: 26 additions & 7 deletions R/ppc-intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,17 @@ ppc_intervals_grouped <- function(y,

#' @rdname PPC-intervals
#' @export
#' @param y_draw For ribbon plots only, a string specifying how to draw `y`. Can
#' be `"line"` (the default), `"points"`, or `"both"`.
ppc_ribbon <- function(y,
yrep,
x = NULL,
...,
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 0.25) {
size = 0.25,
y_draw = c("line", "points", "both")) {
Copy link
Member

@jgabry jgabry Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not essential, but it's common in R to put all valid options in a vector and then use match.arg later to ensure that what the user wrote is a valid option. (See below where I added match.arg)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I was wondering what match.arg does.

check_ignored_arguments(...)

data <- ppc_intervals_data(
Expand All @@ -197,7 +200,8 @@ ppc_ribbon <- function(y,
size = size,
grouped = FALSE,
style = "ribbon",
x_lab = label_x(x)
x_lab = label_x(x),
y_draw = y_draw
)
}

Expand All @@ -213,7 +217,8 @@ ppc_ribbon_grouped <- function(y,
prob = 0.5,
prob_outer = 0.9,
alpha = 0.33,
size = 0.25) {
size = 0.25,
y_draw = c("line", "points", "both")) {
check_ignored_arguments(...)

data <- ppc_intervals_data(
Expand All @@ -234,7 +239,8 @@ ppc_ribbon_grouped <- function(y,
size = size,
grouped = TRUE,
style = "ribbon",
x_lab = label_x(x)
x_lab = label_x(x),
y_draw = y_draw
)
}

Expand Down Expand Up @@ -319,9 +325,11 @@ label_x <- function(x) {
size = 1,
grouped = FALSE,
style = c("intervals", "ribbon"),
x_lab = NULL) {
x_lab = NULL,
y_draw = c("line", "points", "both")) {

style <- match.arg(style)
y_draw <- match.arg(y_draw)
Copy link
Member

@jgabry jgabry Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because the match.arg function will result in an error if the user's y_draw argument isn't one of the three valid ones.


graph <- ggplot(
data = data,
Expand Down Expand Up @@ -349,11 +357,22 @@ label_x <- function(x) {
aes_(color = "yrep"),
size = size/2
) +
geom_blank(aes_(fill = "y")) +
geom_line(
geom_blank(aes_(fill = "y"))

if (y_draw == "line" || y_draw == "both") {
graph <- graph + geom_line(
aes_(y = ~ y_obs, color = "y"),
size = 0.5
)
}

if (y_draw == "points" || y_draw == "both") {
graph <- graph + geom_point(
mapping = aes_(y = ~ y_obs, color = "y", fill = "y"),
shape = 21,
size = 1.5
)
}
} else {
graph <- graph +
geom_pointrange(
Expand Down
9 changes: 7 additions & 2 deletions man/PPC-intervals.Rd

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

2 changes: 1 addition & 1 deletion tests/figs/deps.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- vdiffr-svg-engine: 1.0
- vdiffr: 0.3.2.2
- vdiffr: 0.3.3
- freetypeharfbuzz: 0.2.5
174 changes: 174 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-intervals-y-draw-both.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-intervals-y-draw-line.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-intervals-y-draw-point.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
244 changes: 244 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-ribbon-grouped-default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
236 changes: 236 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-ribbon-grouped-x-values.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
346 changes: 346 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-ribbon-grouped-y-draw-both.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
244 changes: 244 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-ribbon-grouped-y-draw-line.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
340 changes: 340 additions & 0 deletions tests/figs/ppc-intervals-ribbon/ppc-ribbon-grouped-y-draw-point.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/testthat/test-ppc-intervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,40 @@ test_that("ppc_ribbon renders correctly", {

p_50 <- ppc_ribbon(vdiff_y, vdiff_yrep, prob = 0.5)
vdiffr::expect_doppelganger("ppc_ribbon (interval width)", p_50)

p_line <- ppc_ribbon(vdiff_y, vdiff_yrep, y_draw = "line")
vdiffr::expect_doppelganger("ppc_intervals (y_draw = line)", p_line)

p_point <- ppc_ribbon(vdiff_y, vdiff_yrep, y_draw = "point")
vdiffr::expect_doppelganger("ppc_intervals (y_draw = point)", p_point)

p_both <- ppc_ribbon(vdiff_y, vdiff_yrep, y_draw = "both")
vdiffr::expect_doppelganger("ppc_intervals (y_draw = both)", p_both)
})

test_that("ppc_ribbon_grouped renders correctly", {
testthat::skip_on_cran()
testthat::skip_if_not_installed("vdiffr")

p_base <- ppc_ribbon_grouped(vdiff_y, vdiff_yrep, group = vdiff_group)
vdiffr::expect_doppelganger("ppc_ribbon_grouped (default)", p_base)

p_line <- ppc_ribbon_grouped(vdiff_y, vdiff_yrep, group = vdiff_group,
y_draw = "line")
vdiffr::expect_doppelganger("ppc_ribbon_grouped (y_draw = line)", p_line)

p_point <- ppc_ribbon_grouped(vdiff_y, vdiff_yrep, group = vdiff_group,
y_draw = "point")
vdiffr::expect_doppelganger("ppc_ribbon_grouped (y_draw = point)", p_point)

p_both <- ppc_ribbon_grouped(vdiff_y, vdiff_yrep, group = vdiff_group,
y_draw = "both")
vdiffr::expect_doppelganger("ppc_ribbon_grouped (y_draw = both)", p_both)

p_x <- ppc_ribbon_grouped(
y = vdiff_y,
yrep = vdiff_yrep,
x = vdiff_y,
group = vdiff_group)
vdiffr::expect_doppelganger("ppc_ribbon_grouped (x values)", p_x)
})