Skip to content

Commit

Permalink
Merge pull request #88 from UCD-SERG/visualization
Browse files Browse the repository at this point in the history
Visualization
  • Loading branch information
chrisorwa authored Mar 19, 2024
2 parents b898360 + c8986ad commit ab3d01a
Show file tree
Hide file tree
Showing 29 changed files with 165 additions and 100 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Package: serocalculator
Type: Package
Title: Estimating Infection Rates from Serological Data
Version: 1.0.0
Version: 1.0.0.9000
Authors@R: c(
person(given = "Peter", family = "Teunis", email = "[email protected]", role = c("aut", "cph"), comment = "Author of the method and original code."),
person(given = "Kristina", family = "Lai", email = "[email protected]", role = c("aut", "cre")),
person(given = "Chris", family = "Orwa", role = "aut"),
person(given = "Kristen", family = "Aiemjoy", email = "[email protected]", role = c("aut")),
person(given = "Douglas Ezra", family = "Morrison", email = "[email protected]", role = c("aut")))
Description: Translates antibody levels measured in cross-sectional population
Expand All @@ -28,7 +29,9 @@ Imports:
stats,
tibble,
tidyr,
utils
utils,
ggfortify,
cli
Suggests:
parallel,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ importFrom(rngtools,RNGseq)
importFrom(rngtools,setRNG)
importFrom(stats,dlnorm)
importFrom(stats,formula)
importFrom(stats,lm)
importFrom(stats,median)
importFrom(stats,nlm)
importFrom(stats,optim)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# serocalculator (development version)

* Added `type = "age-scatter"` option for `autoplot.pop_data()`

## serocalculator 1.0.0

* Moved underlying methods to `serocalculator` vignette
Expand Down
115 changes: 82 additions & 33 deletions R/autoplot.pop_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,117 @@
#' @param log whether to show antibody responses on logarithmic scale
#' @param strata the name of a variable in `pop_data` to stratify by (or `NULL` for no stratification)
#' @param ... unused
#' @param type an option to choose type of chart: the current options are `"density"` or `"age-scatter"`
#'
#' @return a [ggplot2::ggplot] object
#' @export
#'
#' @examples
#' library(dplyr)
#' library(ggplot2)
#' library(ggfortify)
#'
#' xs_data <- "https://osf.io/download//n6cp3/" %>%
#' load_pop_data() %>%
#' clean_pop_data()
#'
#' xs_data %>% autoplot(strata = "Country")
#' load_pop_data() %>%
#' clean_pop_data()
#'
autoplot.pop_data = function(
#' xs_data %>% autoplot(strata = "Country", type = "density")
#' xs_data %>% autoplot(strata = "Country", type = "age-scatter")
#' @export
autoplot.pop_data <- function(
object,
log = FALSE,
log = FALSE,
type = "density",
strata = NULL,
...)
{
...) {

if (type == "age-scatter") {
age_scatter(object, strata)
} else if (type == "density") {
density_plot(object, strata, log)
} else {
cli::cli_abort(
'`type = "{type}"` is not a valid input;
the currently available options for `type` are "density" or "age-scatter"')
}
}

age_scatter <- function(
object,
strata = NULL) {
# create default plotting

if(is.null(strata))
{
plot1 <-
object %>%
ggplot2::ggplot(aes(x = .data$age, y = .data$value))
} else
{
plot1 <-
object %>%
ggplot2::ggplot(aes(x = .data$age, y = .data$value, col = get(strata))) +
ggplot2::labs(colour = strata)
}

plot1 <- plot1 +
ggplot2::theme_linedraw() +
ggplot2::scale_y_log10() +
ggplot2::geom_point(size = .6, alpha = .7) +
ggplot2::geom_smooth(
method = "lm",
se = FALSE,
formula = y ~ x,
na.rm = TRUE) +
ggplot2::labs(
title = "Quantitative Antibody Responses by Age",
x = "Age",
y = "Antibody Response Value"
)

return(plot1)
}

plot1 =
# density plotting function
density_plot <- function(
object,
strata = NULL,
log = FALSE) {
plot1 <-
object %>%
ggplot2::ggplot(aes(x = .data$value)) +
ggplot2::theme_minimal() +
ggplot2::theme_linedraw() +
ggplot2::facet_wrap(~antigen_iso, nrow = 3)

if(!is.null(strata))
{
plot1 = plot1 +
if (is.null(strata)) {
plot1 <- plot1 +
ggplot2::geom_density(
ggplot2::aes(fill = get(strata)),
alpha = .6,
color = "black")
} else
{
plot1 = plot1 +
color = "black"
)
} else {
plot1 <- plot1 +
ggplot2::geom_density(
# aes(fill = get(strata)),
alpha = .6,
color = "black")
}


if(log)
{
plot1 = plot1 +
color = "black",
aes(fill = get(strata))
) +
ggplot2::labs(fill = strata)
}
if (log) {
plot1 <- plot1 +
ggplot2::scale_x_log10() +
ggplot2::labs(
title = "Distribution of Cross-sectional Antibody Responses (Log transformed)",
x = "Log10(Antibody Response Value)",
y = "Frequency"
)
} else
{
plot = plot1 +
} else {
plot1 <- plot1 +
ggplot2::labs(
title = "Distribution of Cross-sectional Antibody Responses",
x = "Antibody Response Value",
y = "Frequency"
)

}

return(plot1)
}
}
3 changes: 2 additions & 1 deletion R/plot.seroincidence.est.R → R/autoplot.seroincidence.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ autoplot.seroincidence =
{
to_return = to_return +
ggplot2::scale_x_log10(
labels = scales::label_comma())
labels = scales::label_comma()) +
ggplot2::theme_linedraw()
}

return(to_return)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ autoplot.summary.seroincidence.by = function(
alpha = alpha) +
ggplot2::xlab(xvar) +
ggplot2::ylab("Estimated incidence rate") +
ggplot2::theme_bw() +
ggplot2::theme_linedraw() +
ggplot2::expand_limits(x = 0, y = 0) +
ggplot2::labs(col = "`nlm()` convergence code") +
ggplot2::theme(legend.position="bottom")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions R/graph.decay.curves.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ plot_curve_params_one_ab = function(
minor_breaks = NULL
) +
# ggplot2::scale_x_log10() +
ggplot2::theme_minimal() +
ggplot2::theme_linedraw() +
ggplot2::theme(
axis.line = ggplot2::element_line()) +
ggplot2::labs(
x = "Days since fever onset",
y = "ELISA units")
y = "Antibody Concentration") +
ggplot2::ggtitle('Decay Curve') +
ggplot2::theme(plot.title = ggplot2::element_text(size = 20, face = "bold"))

layer_function = function(cur_row)
{
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 7 additions & 12 deletions R/print.seroincidence.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' @title
#' Print Method for `seroincidence.by` Object
#' Print Method for `seroincidence` Object
#'
#' @description
#' Custom [print()] function to show output of the seroincidence calculator [est.incidence.by()].
#' Custom [print()] function to show output of the seroincidence calculator [est.incidence()].
#'
#' @param x A list containing output of function [est.incidence.by()].
#' @param ... Additional arguments affecting the summary produced.
Expand All @@ -21,17 +21,12 @@
#' }
#'
#' @export
print.seroincidence.by <- function(x, ...)
print.seroincidence <- function(x, ...)
{
cat("`seroincidence.by` object estimated given the following setup:\n")
cat(paste("a) Antigen isotypes :", paste(attr(x, "antigen_isos"), collapse = ", ")), "\n")
cat(paste("b) Strata :", paste(attr(x, "Strata") %>% attr("strata_vars"), collapse = ", ")), "\n")

cat("\n")
cat("This object is a list of `seroincidence` objects, with added meta-data attributes:")
cat("`antigen_isos` - Character vector of antigen isotypes used in analysis.\n")
cat("`Strata` - Input parameter strata of function `est.incidence.by()`\n")
cat("\n")
cat("`seroincidence` object estimated given the following setup:\n")
cat(paste("a) `antigen_isos`: ", paste(attr(x, "antigen_isos"), collapse = ", ")), "\n")
cat(paste("b) `lambda_start`: ", attr(x, "lambda_start"), "\n"))
cat("Call the `summary()` function to obtain output results.\n")
cat("Call the `autoplot()` function to graph the log-likelihood curve.\n")
invisible(x)
}
37 changes: 37 additions & 0 deletions R/print.seroincidence.by.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' @title
#' Print Method for `seroincidence.by` Object
#'
#' @description
#' Custom [print()] function to show output of the seroincidence calculator [est.incidence.by()].
#'
#' @param x A list containing output of function [est.incidence.by()].
#' @param ... Additional arguments affecting the summary produced.
#'
#' @examples
#'
#' \dontrun{
#' # estimate seroincidence
#' seroincidence <- est.incidence.by(...)
#'
#' # print the seroincidence object to the console
#' print(seroincidence)
#'
#' # or simply type (appropriate print method will be invoked automatically)
#' seroincidence
#' }
#'
#' @export
print.seroincidence.by <- function(x, ...)
{
cat("`seroincidence.by` object estimated given the following setup:\n")
cat(paste("a) Antigen isotypes :", paste(attr(x, "antigen_isos"), collapse = ", ")), "\n")
cat(paste("b) Strata :", paste(attr(x, "Strata") %>% attr("strata_vars"), collapse = ", ")), "\n")

cat("\n")
cat("This object is a list of `seroincidence` objects, with added meta-data attributes:")
cat("`antigen_isos` - Character vector of antigen isotypes used in analysis.\n")
cat("`Strata` - Input parameter strata of function `est.incidence.by()`\n")
cat("\n")
cat("Call the `summary()` function to obtain output results.\n")
invisible(x)
}
32 changes: 0 additions & 32 deletions R/print.seroincidence.est.R

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions R/serocalculator-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#' @importFrom rngtools setRNG
#' @importFrom stats dlnorm optim pgamma plnorm
#' @importFrom stats formula
#' @importFrom stats lm
#' @importFrom stats median
#' @importFrom stats nlm
#' @importFrom stats qnorm
Expand Down
13 changes: 8 additions & 5 deletions man/autoplot.pop_data.Rd

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

2 changes: 1 addition & 1 deletion man/autoplot.seroincidence.Rd

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

2 changes: 1 addition & 1 deletion man/autoplot.seroincidence.by.Rd

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

2 changes: 1 addition & 1 deletion man/autoplot.summary.seroincidence.by.Rd

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

Loading

0 comments on commit ab3d01a

Please sign in to comment.