Skip to content

Commit

Permalink
Plot coverage (#46)
Browse files Browse the repository at this point in the history
* Add a function to plot the coverage

* Add tests for coverage plots

* Add function to `{pkgdown}` site

* Update NEWS
  • Loading branch information
arisp99 authored Jul 31, 2022
1 parent c6a6605 commit f8491e5
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ S3method(mutation_prevalence,geno_tbl)
S3method(mutation_prevalence,ref_alt_cov_tbl)
S3method(plot,mut_freq)
S3method(plot,mut_prev)
S3method(plot_coverage,cov_tbl)
S3method(plot_coverage,default)
S3method(plot_coverage,ref_alt_cov_tbl)
S3method(tbl_sum,alt_tbl)
S3method(tbl_sum,cov_tbl)
S3method(tbl_sum,geno_tbl)
Expand Down Expand Up @@ -65,6 +68,7 @@ export(miplicorn_example)
export(mutation_frequency)
export(mutation_prevalence)
export(plot_chromoMap)
export(plot_coverage)
export(plot_karyoploteR)
export(plot_mutation_frequency)
export(plot_mutation_prevalence)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# miplicorn (development version)

- New `plot_coverage()` function can be used to plot the average coverage across
a given grouping variable (#46).
- The column created by `label_mutations()` has been renamed from
`ans_der_indel` to `mutation_label` (#44).
- `label_mutations()` more clearly signals when the computation can not be
Expand Down
67 changes: 67 additions & 0 deletions R/plot_coverage.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#------------------------------------------------
#' Plot coverage
#'
#' Create a bar chart visualizing the average coverage across a given grouping
#' variable. The color of each bar represents the magnitude of the mean
#' coverage.
#'
#' @param data A data frame, data frame extension (e.g. a tibble), or a lazy
#' data frame (e.g. from dbplyr or dtplyr).
#' @param group_by The grouping variable. The average coverage is computed for
#' each unique value of the variable.
#'
#' @export
#' @examples
#' # Read example data
#' data <- read_tbl_coverage(miplicorn_example("coverage_AA_table.csv"))
#'
#' # Plot coverage grouped by gene
#' plot_coverage(data, gene)
#' @export
plot_coverage <- function(data, group_by) {
UseMethod("plot_coverage")
}

#' @export
plot_coverage.default <- function(data, group_by) {
cli_abort(c(
"Cannot plot the coverage with this data object.",
"i" = "Object must be a coverage table or a reference, alternate, coverage table."
))
}

# Plotting function
plot_coverage_fn <- function(data, group_by) {
# Ensure inputs exist
rlang::check_required(data)
rlang::check_required(group_by)

# Group and create stats
plot_data <- data %>%
dplyr::group_by({{ group_by }}) %>%
dplyr::summarise(mean_coverage = mean(.data$coverage, na.rm = TRUE))

# Plot
ggplot2::ggplot(
data = plot_data,
mapping = ggplot2::aes(
x = {{ group_by }},
y = .data$mean_coverage,
fill = .data$mean_coverage
)
) +
ggplot2::geom_col() +
default_theme() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(angle = 90, vjust = 0.5, hjust = 1),
legend.position = "none"
)
}

#' @rdname plot_coverage
#' @export
plot_coverage.cov_tbl <- plot_coverage_fn

#' @rdname plot_coverage
#' @export
plot_coverage.ref_alt_cov_tbl <- plot_coverage_fn
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ reference:
These functions are used to visualize data.
contents:
- chromosome-map
- plot_coverage
- plot_mutation_prevalence
- plot_mutation_frequency
- theme_miplicorn
Expand Down
33 changes: 33 additions & 0 deletions man/plot_coverage.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/_snaps/plot_coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# data must have mut_prev class

Cannot plot the coverage with this data object.
i Object must be a coverage table or a reference, alternate, coverage table.

99 changes: 99 additions & 0 deletions tests/testthat/_snaps/plot_coverage/can-add-ggplot2-layers.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f8491e5

Please sign in to comment.