Skip to content

Commit

Permalink
fix: bug in mirror plot for 2D data
Browse files Browse the repository at this point in the history
also added warning when automatically selecting factor levels
  • Loading branch information
ethanbass committed Apr 2, 2024
1 parent 8d46dd7 commit 527520c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# chromatographR 0.7.1

* Fixed bug in `get_peaks` causing peaks to erroneously filtered out in some cases.
* Small updates to documentation (in `preprocess` and `fit_peaks` functions) to better describe arguments.
* Made small updates to documentation (in `preprocess` and `fit_peaks` functions) to better describe arguments.
* Added warning in `mirror_plot` when `var` contains more than two levels and levels aren't specified.
* Fixed bug to allow `mirror_plot` to work properly with 2D data.

# chromatographR 0.7.0

Expand Down
16 changes: 11 additions & 5 deletions R/plot.peak_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ boxplot.peak_table <- function(x, formula, ...){
#' @concept Visualization
#' @export

mirror_plot <- function(x, chrom_list, lambdas, var, subset = NULL,
mirror_plot <- function(x, chrom_list, lambdas = NULL, var, subset = NULL,
print_legend = TRUE, legend_txt = NULL, legend_pos = "topright",
legend_size = 1, mirror = TRUE, xlim = NULL, ylim = NULL, ...){
check_peaktable(x)
Expand All @@ -214,9 +214,11 @@ mirror_plot <- function(x, chrom_list, lambdas, var, subset = NULL,
}
if (missing(chrom_list)){
chrom_list <- get_chrom_list(x)
} else get_chrom_list(x, chrom_list)
} else {
get_chrom_list(x, chrom_list)
}
new.ts <- round(as.numeric(rownames(chrom_list[[1]])), 2)
if (ncol(x[[1]]) == 1){
if (ncol(chrom_list[[1]]) == 1){
lambdas.idx <- 1
} else {
lambdas.idx <- sapply(lambdas, function(lambda){
Expand All @@ -234,6 +236,10 @@ mirror_plot <- function(x, chrom_list, lambdas, var, subset = NULL,
}
}
if (is.null(subset)){
if (nlevels(fac)>2){
warning(paste0("Selecting first two levels of ", sQuote(var)), ". To choose different levels,
use the `subset` argument to specify the desired levels.")
}
trt1 <- levels(fac)[1]
trt2 <- levels(fac)[2]
} else {
Expand All @@ -245,9 +251,9 @@ mirror_plot <- function(x, chrom_list, lambdas, var, subset = NULL,
if (print_legend & is.null(legend_txt))
legend_txt <- c(trt1,trt2)
if (is.null(xlim))
xlim <- c(head(new.ts,1),tail(new.ts,1))
xlim <- c(head(new.ts, 1), tail(new.ts, 1))
y_max <- max(sapply(chrom_list, function(xx){
max(xx[, as.character(min(as.numeric(lambdas)))], na.rm = TRUE)
max(xx[, lambdas.idx], na.rm = TRUE)
}))
if (mirror){
if (is.null(ylim))
Expand Down
2 changes: 1 addition & 1 deletion man/mirror_plot.Rd

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

7 changes: 6 additions & 1 deletion tests/testthat/test-pipeline.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ test_that("attach_metadata works", {
expect_equal(colnames(pk_tab$sample_meta), colnames(meta))

### check errors & warnings ###
expect_error(attach_metadata(pk_tab))
expect_error(attach_metadata())
expect_error(attach_metadata(warp))
expect_error(attach_metadata(pk_tab, metadata = warp))
expect_error(attach_metadata(pk_tab, metadata=meta, column ="x"))
expect_error(attach_metadata(pk_tab$tab, metadata=meta, column ="vial"))
expect_error(attach_metadata(pk_tab, metadata=rbind(meta,meta), column ="vial"))
Expand All @@ -217,6 +219,9 @@ test_that("attach_ref_spectra works", {
pk_tab <- attach_ref_spectra(pk_tab, chrom_list = dat.pr, ref = "max.int")
expect_equal(pk_tab$args[["reference_spectra"]], "max.int")
expect_equal(colnames(pk_tab$tab), colnames(pk_tab$ref_spectra))

# test errors
expect_error(attach_ref_spectra(peak_table = warp))
expect_error(attach_ref_spectra(pk_tab, chrom_list = dat.pr, ref = "x"))
})

Expand Down

0 comments on commit 527520c

Please sign in to comment.