Skip to content

Commit

Permalink
Merge pull request #81 from gthopkins/main
Browse files Browse the repository at this point in the history
Add plotting function for radEmu output
  • Loading branch information
svteichman authored Sep 4, 2024
2 parents 8ab0fd7 + 12e6e05 commit cae1f18
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 73 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ Depends:
MASS,
Matrix,
methods,
dplyr,
ggplot2,
magrittr,
rlang,
R (>= 2.10)
Suggests:
testthat (>= 3.0.0),
numDeriv,
phyloseq,
knitr,
magrittr,
dplyr,
ggplot2,
stringr,
parallel,
rmarkdown
Expand Down
7 changes: 4 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,radEmu)
S3method(plot,emuFit)
S3method(print,emuFit)
export(emuFit)
export(emuFit_micro)
export(score_test)
export(simulate_data)
import(MASS)
import(Matrix)
import(dplyr)
import(ggplot2)
importFrom(magrittr,"%>%")
importFrom(methods,as)
importFrom(rlang,.data)
importFrom(stats,cov)
Expand All @@ -19,7 +18,9 @@ importFrom(stats,optim)
importFrom(stats,pchisq)
importFrom(stats,qnorm)
importFrom(stats,rbinom)
importFrom(stats,reorder)
importFrom(stats,rnbinom)
importFrom(stats,rnorm)
importFrom(stats,rpois)
importFrom(stats,setNames)
importFrom(stats,weighted.mean)
93 changes: 47 additions & 46 deletions R/plot_radEmu.R → R/plot_emuFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#' @param display_taxon_names (Optional). Default \code{TRUE}. Boolean. If \code{FALSE}, remove sample names from the plot.
#' @param data_only (Optional). Default \code{FALSE}. Boolean. If \code{TRUE}, only returns data frame.
#' @param ... There are no optional parameters at this time.
#' @import dplyr
#' @import ggplot2
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @importFrom stats reorder
#' @importFrom stats setNames
#'
#' @return Object of class \code{ggplot}. Plot of \code{radEmu} model fit with 95% confidence intervals.
#'
Expand All @@ -23,13 +24,13 @@
#'
#' mOTU_names <- colnames(wirbel_otu)
#' mOTU_name_df <- data.frame(name = mOTU_names) %>%
#' mutate(base_name = stringr::str_remove(mOTU_names, "unknown ") %>%
#' dplyr::mutate(base_name = stringr::str_remove(mOTU_names, "unknown ") %>%
#' stringr::str_remove("uncultured ")) %>%
#' mutate(genus_name = stringr::word(base_name, 1))
#' dplyr::mutate(genus_name = stringr::word(base_name, 1))
#'
#' restricted_mOTU_names <- mOTU_name_df %>%
#' filter(genus_name %in% chosen_genera) %>%
#' pull(name)
#' dplyr::filter(genus_name %in% chosen_genera) %>%
#' dplyr::pull(name)
#'
#' small_Y <- wirbel_otu[subset_studies, restricted_mOTU_names]
#' category_to_rm <- which(colSums(small_Y) == 0)
Expand All @@ -45,15 +46,15 @@
#' p2 = c("FR-Control" = "StudyFR-CRC",
#' "US-Control" = "StudyUS-CRC"))
#'
#' out <- plot.radEmu(x = ch_fit,
#' plot_key = plot_key,
#' display_taxon_names = FALSE)
#' out <- plot(x = ch_fit,
#' plot_key = plot_key,
#' display_taxon_names = FALSE)
#'
#' out$plots$p1
#' out$plots$p2
#' @export

plot.radEmu <- function(x,
plot.emuFit <- function(x,
plot_key = NULL,
title = NULL,
taxon_names = NULL,
Expand Down Expand Up @@ -104,9 +105,9 @@ plot.radEmu <- function(x,
new_variable_names <- names(unlist(unname(plot_key_default)))
old_variable_names <- unname(unlist(unname(plot_key_default)))
mod$coef <- mod$coef %>%
mutate(covariate = factor(covariate,
levels = old_variable_names,
labels = new_variable_names))
dplyr::mutate(covariate = factor(covariate,
levels = old_variable_names,
labels = new_variable_names))

# now separate output coefficients into a list for each plot
coef_list <- mod$coef %>%
Expand All @@ -115,49 +116,49 @@ plot.radEmu <- function(x,
# match coefficient names using user-provided "taxon_names" data frame
coef_list_renamed <- lapply(coef_list, function(coef_subset){
if (!is.null(taxon_names)) {
left_join(coef_subset, taxon_names, by = "category") %>%
arrange(covariate, estimate) %>%
mutate(cat_small = reorder(cat_small, estimate))
dplyr::left_join(coef_subset, taxon_names, by = "category") %>%
dplyr::arrange(covariate, estimate) %>%
dplyr::mutate(cat_small = reorder(cat_small, estimate))
} else {
coef_subset %>%
mutate(cat_small = category) %>%
arrange(covariate, estimate) %>%
mutate(cat_small = factor(cat_small,
levels = unique(cat_small),
labels = unique(cat_small)))
dplyr::mutate(cat_small = category) %>%
dplyr::arrange(covariate, estimate) %>%
dplyr::mutate(cat_small = factor(cat_small,
levels = unique(cat_small),
labels = unique(cat_small)))
}
})

# return plots
if (!data_only) {

p_list <- lapply(coef_list_renamed, function(coef_subset){
p <- ggplot(coef_subset) +
geom_point(aes(x = estimate,
y = as.character(cat_small),
color = covariate,
group = covariate),
position = position_dodge2(width = 0.5),
size = 2) +
geom_errorbar(aes(y = cat_small,
xmin = lower,
xmax = upper,
color = covariate,
group = covariate),
position = position_dodge2(width = 0.5),
width = 0.5) +
geom_vline(xintercept = 0, alpha = 0.5) +
theme_bw() +
labs(title = title) +
guides(color = guide_legend(title = "Comparison")) +
theme(legend.position = "bottom") +
labs(y = "Category") +
labs(x = "Estimate")
p <- ggplot2::ggplot(coef_subset) +
ggplot2::geom_point(ggplot2::aes(x = estimate,
y = as.character(cat_small),
color = covariate,
group = covariate),
position = ggplot2::position_dodge2(width = 0.5),
size = 2) +
ggplot2::geom_errorbar(ggplot2::aes(y = cat_small,
xmin = lower,
xmax = upper,
color = covariate,
group = covariate),
position = ggplot2::position_dodge2(width = 0.5),
width = 0.5) +
ggplot2::geom_vline(xintercept = 0, alpha = 0.5) +
ggplot2::theme_bw() +
ggplot2::labs(title = title) +
ggplot2::guides(color = ggplot2::guide_legend(title = "Comparison")) +
ggplot2::theme(legend.position = "bottom") +
ggplot2::labs(y = "Category") +
ggplot2::labs(x = "Estimate")

if (!display_taxon_names) {
p <- p +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank())
ggplot2::theme(axis.text.y = ggplot2::element_blank(),
axis.ticks.y = ggplot2::element_blank())
}

p
Expand All @@ -166,13 +167,13 @@ plot.radEmu <- function(x,
p_list <- setNames(p_list, paste0("p", 1:length(p_list)))

return(list(plots = invisible(p_list),
data = bind_rows(coef_list_renamed)))
data = dplyr::bind_rows(coef_list_renamed)))

} else {

# return data
return(list(plots = NULL,
data = bind_rows(coef_list_renamed)))
data = dplyr::bind_rows(coef_list_renamed)))
}
}

Expand Down
22 changes: 11 additions & 11 deletions man/plot.radEmu.Rd → man/plot.emuFit.Rd

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ fitted_model <- emuFit(Y = Y,
use_fullmodel_cov = FALSE,
return_both_score_pvals = FALSE)

test_that("plot.radEmu returns data frame and plot", {
plot_out <- plot.radEmu(x = fitted_model)
test_that("plot() returns data frame and plot", {
plot_out <- plot(x = fitted_model)
expect_true(is.data.frame(plot_out$data))
expect_true(all(sapply(plot_out$plots, is.ggplot)))
expect_true(all(sapply(plot_out$plots, ggplot2::is.ggplot)))
})


test_that("plot.radEmu returns error when plot_key does not match coefficient table", {
test_that("plot() returns error when plot_key does not match coefficient table", {
expect_error({
plot.radEmu(x = fitted_model,
plot_key = list(c("First Covariate" = "covariate1")))
plot(x = fitted_model,
plot_key = list(c("First Covariate" = "covariate1")))
})
})

test_that("plot.radEmu returns error when coefficient is included multiple times in plot_key", {
test_that("plot() returns error when coefficient is included multiple times in plot_key", {
expect_error({
plot.radEmu(x = fitted_model,
plot_key = list(c("First Covariate" = "covariate_1"),
c("Second Covariate" = "covariate_1")))
plot(x = fitted_model,
plot_key = list(c("First Covariate" = "covariate_1"),
c("Second Covariate" = "covariate_1")))
})
})

Expand Down

0 comments on commit cae1f18

Please sign in to comment.