Skip to content

Commit 0f7a05a

Browse files
authored
Merge pull request #455 from UCD-SERG/update_graph_curve2
Add chain_color option to graph.curve.params() for customizing MCMC line color
2 parents 3c464f1 + a5396ea commit 0f7a05a

7 files changed

Lines changed: 359 additions & 9 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: serocalculator
33
Title: Estimating Infection Rates from Serological Data
4-
Version: 1.3.0.9057
4+
Version: 1.3.0.9058
55
Authors@R: c(
66
person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"),
77
comment = "Author of the method and original code."),

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## New features
44

5+
* Added `chain_color` option to `graph.curve.params()` to control MCMC line color (#455)
56
* Made `graph.curve.params()` the default sub-method for `autoplot.curve_params()` (#450)
67
* Added `log_x` and `log_y` options to `graph.curve.params()` sub-method for
78
`autoplot.curve_params()` (#453)

R/graph.curve.params.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#' or linear scale (`FALSE`, default)?
1515
#' @param log_y should the Y-axis be on a logarithmic scale
1616
#' (default, `TRUE`) or linear scale (`FALSE`)?
17+
#' @param chain_color [logical]: if [TRUE] (default), MCMC chain lines
18+
#' are colored by chain.
19+
#' If [FALSE], all MCMC chain lines are black.
1720
#' @inheritParams plot_curve_params_one_ab
1821
#' @param ... not currently used
1922
#' @returns a [ggplot2::ggplot()] object showing the antibody dynamic
@@ -31,6 +34,7 @@ graph.curve.params <- function( # nolint: object_name_linter
3134
verbose = FALSE,
3235
quantiles = c(0.1, 0.5, 0.9),
3336
alpha_samples = 0.3,
37+
chain_color = TRUE,
3438
log_x = FALSE,
3539
log_y = TRUE,
3640
n_curves = 100,
@@ -150,14 +154,13 @@ graph.curve.params <- function( # nolint: object_name_linter
150154
mutate(
151155
group = interaction(across(all_of(group_vars)))
152156
)
153-
154157
plot1 <-
155158
plot1 +
156159
geom_line(
157160
data = sc_to_graph,
158161
alpha = alpha_samples,
159162
aes(
160-
color = .data$chain |> factor(),
163+
color = if (chain_color) .data$chain |> factor(),
161164
group = .data$group
162165
)
163166
)
@@ -201,10 +204,12 @@ graph.curve.params <- function( # nolint: object_name_linter
201204
linewidth = 0.75
202205
)
203206

204-
if (length(iters_to_graph) > 0) {
205-
plot1 <- plot1 +
206-
ggplot2::labs(col = "MCMC chain")
207+
label <- if (length(iters_to_graph) > 0 && chain_color) {
208+
"MCMC chain"
209+
} else {
210+
""
207211
}
212+
plot1 <- plot1 + ggplot2::labs(col = label)
208213
}
209214

210215

inst/examples/exm-graph.curve.params.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ plot2 <- graph.curve.params(
1313
quantiles = c(0.1, 0.5, 0.9)
1414
)
1515
print(plot2)
16+
17+
# Plot with MCMC chains in black
18+
plot3 <- graph.curve.params(
19+
curve,
20+
n_curves = Inf,
21+
quantiles = c(0.1, 0.5, 0.9),
22+
chain_color = FALSE
23+
)
24+
print(plot3)

man/graph.curve.params.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/graph.curve.params/curve-black-chains.svg

Lines changed: 312 additions & 0 deletions
Loading

tests/testthat/test-graph.curve.params.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test_that(
1919
)
2020
plot2 |> vdiffr::expect_doppelganger(title = "curve-quantiles-and-samples")
2121

22-
# 3. Curves only, no quantiles
22+
# 3. Test that disabling quantiles works correctly (curves only)
2323
plot3 <- graph.curve.params(
2424
curve,
2525
n_curves = Inf,
@@ -28,14 +28,22 @@ test_that(
2828

2929
plot3 |> vdiffr::expect_doppelganger(title = "curve-samples")
3030

31-
# 4. Custom numeric quantiles only
31+
# 4. Test that custom numeric quantiles are drawn correctly
3232
plot4 <- graph.curve.params(
3333
curve,
3434
n_curves = 0,
3535
quantiles = c(0.05, 0.55, 0.95)
3636
)
3737
plot4 |> vdiffr::expect_doppelganger(title = "curve-custom-quantiles")
3838

39+
# 5. Test that chain_color = FALSE works correctly
40+
plot5 <- graph.curve.params(
41+
curve,
42+
n_curves = Inf,
43+
quantiles = c(0.05, 0.55, 0.95),
44+
chain_color = FALSE
45+
)
46+
plot5 |> vdiffr::expect_doppelganger(title = "curve-black-chains")
3947
}
4048
)
4149

@@ -46,10 +54,11 @@ test_that(
4654
typhoid_curves_nostrat_100 |>
4755
dplyr::filter(antigen_iso %in% c("HlyE_IgA", "HlyE_IgG")) |>
4856
dplyr::mutate(.by = antigen_iso, chain = rep(1:2, times = n() / 2))
57+
58+
# Test that log_x argument works with samples only
4959
plot3 <- graph.curve.params(
5060
curve,
5161
n_curves = Inf,
52-
show_quantiles = TRUE,
5362
log_x = TRUE
5463
)
5564
plot3 |>

0 commit comments

Comments
 (0)