From 705956858188793c2d61e5e8727ed4da3e3720d6 Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Sun, 3 Dec 2023 16:51:54 +0100 Subject: [PATCH] fix scale functions --- DESCRIPTION | 2 +- NEWS.md | 8 ++++++-- R/plot.R | 21 ++++++++++++-------- inst/tinytest/test-_deprecated.R | 34 -------------------------------- inst/tinytest/test-zzz.R | 17 ++++++++++++++-- man/plot.Rd | 10 ++++++---- 6 files changed, 41 insertions(+), 51 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c4ee124c..34fded0a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: AMR -Version: 2.1.1.9002 +Version: 2.1.1.9003 Date: 2023-12-03 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) diff --git a/NEWS.md b/NEWS.md index e808d831..2a40d8ba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,11 @@ -# AMR 2.1.1.9002 +# AMR 2.1.1.9003 + +## Breaking +* Removed all functions and references that used the deprecated `rsi` class, which were all replaced with their `sir` equivalents a year ago ## New -* Function `scale_x_mic()`, an advanced function to use in ggplot, to allow plotting of MIC values on the x axis. It allow for manual range definition and plotting missing intermediate log2 levels. +* Function group `scale_*_mic()`, namely: `scale_x_mic()`, `scale_y_mic()`, `scale_colour_mic()` and `scale_fill_mic()`. They are advanced ggplot2 extensions to allow plotting of MIC values. They allow for manual range definition and plotting missing intermediate log2 levels. +* Function `rescale_mic()`, which allows to rescale MIC values to a manually set range. This is the powerhouse behind the `scale_*_mic()` functions, but it can be used by users directly to e.g. compare equality in MIC distributions by rescaling them to the same range first. ### Changed * For MICs: diff --git a/R/plot.R b/R/plot.R index a9e7f71d..847fc017 100755 --- a/R/plot.R +++ b/R/plot.R @@ -110,10 +110,12 @@ NULL #' @export #' @inheritParams as.mic +#' @param drop a [logical] to remove intermediate MIC values, defaults to `FALSE` #' @rdname plot -scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { +scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { stop_ifnot_installed("ggplot2") - scale <- ggplot2::scale_x_discrete(...) + meet_criteria(drop, allow_class = "logical", has_length = 1) + scale <- ggplot2::scale_x_discrete(drop = drop, ...) scale$transform <- function(x, keep_ops = keep_operators, mic_rng = mic_range) { rescale_mic(x = x, keep_operators = keep_ops, mic_range = mic_rng, as.mic = FALSE) } @@ -123,9 +125,10 @@ scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { #' @export #' @inheritParams as.mic #' @rdname plot -scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { +scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { stop_ifnot_installed("ggplot2") - scale <- ggplot2::scale_y_discrete(...) + meet_criteria(drop, allow_class = "logical", has_length = 1) + scale <- ggplot2::scale_y_discrete(drop = drop, ...) scale$transform <- function(x, keep_ops = keep_operators, mic_rng = mic_range) { rescale_mic(x = x, keep_operators = keep_ops, mic_range = mic_rng, as.mic = FALSE) } @@ -135,9 +138,10 @@ scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { #' @export #' @inheritParams as.mic #' @rdname plot -scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { +scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { stop_ifnot_installed("ggplot2") - scale <- ggplot2::scale_colour_discrete(...) + meet_criteria(drop, allow_class = "logical", has_length = 1) + scale <- ggplot2::scale_colour_discrete(drop = drop, ...) scale$transform <- function(x, keep_ops = keep_operators, mic_rng = mic_range) { rescale_mic(x = x, keep_operators = keep_ops, mic_range = mic_rng, as.mic = FALSE) } @@ -147,9 +151,10 @@ scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { #' @export #' @inheritParams as.mic #' @rdname plot -scale_fill_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { +scale_fill_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { stop_ifnot_installed("ggplot2") - scale <- ggplot2::scale_fill_discrete(...) + meet_criteria(drop, allow_class = "logical", has_length = 1) + scale <- ggplot2::scale_fill_discrete(drop = drop, ...) scale$transform <- function(x, keep_ops = keep_operators, mic_rng = mic_range) { rescale_mic(x = x, keep_operators = keep_ops, mic_range = mic_rng, as.mic = FALSE) } diff --git a/inst/tinytest/test-_deprecated.R b/inst/tinytest/test-_deprecated.R index 8c7a629d..575ce874 100644 --- a/inst/tinytest/test-_deprecated.R +++ b/inst/tinytest/test-_deprecated.R @@ -27,38 +27,4 @@ # how to conduct AMR data analysis: https://msberends.github.io/AMR/ # # ==================================================================== # -sir <- random_sir(100) -rsi <- sir -class(rsi) <- gsub("sir", "rsi", class(rsi)) -mic <- random_mic(100) -disk <- random_disk(100) -expect_identical(summary(sir), summary(rsi)) -expect_identical(c(sir), c(rsi)) - -expect_identical(suppressWarnings(suppressMessages(as.rsi(as.character(rsi)))), - suppressWarnings(suppressMessages(as.sir(as.character(sir))))) -expect_identical(suppressWarnings(suppressMessages(as.rsi(mic, mo = "Escherichia coli", ab = "CIP"))), - suppressWarnings(suppressMessages(as.sir(mic, mo = "Escherichia coli", ab = "CIP")))) -expect_identical(suppressWarnings(suppressMessages(as.rsi(disk, mo = "Escherichia coli", ab = "CIP"))), - suppressWarnings(suppressMessages(as.sir(disk, mo = "Escherichia coli", ab = "CIP")))) -expect_identical(suppressWarnings(suppressMessages(as.rsi(data.frame(CIP = mic, mo = "Escherichia coli")))), - suppressWarnings(suppressMessages(as.sir(data.frame(CIP = mic, mo = "Escherichia coli"))))) - -expect_identical(suppressWarnings(n_rsi(example_isolates$CIP)), - suppressWarnings(n_sir(example_isolates$CIP))) - -expect_identical(suppressWarnings(rsi_df(example_isolates)), - suppressWarnings(sir_df(example_isolates))) - -expect_identical(suppressWarnings(is.rsi.eligible(example_isolates)), - suppressWarnings(is_sir_eligible(example_isolates))) - -if (AMR:::pkg_is_available("ggplot2")) { - expect_equal(suppressWarnings(ggplot_rsi(example_isolates[, c("CIP", "GEN", "TOB")])), - suppressWarnings(ggplot_sir(example_isolates[, c("CIP", "GEN", "TOB")]))) - - p <- ggplot2::ggplot(example_isolates[, c("CIP", "GEN", "TOB")]) - expect_equal(suppressWarnings(p + geom_rsi() + scale_rsi_colours() + labels_rsi_count() + facet_rsi() + theme_rsi()), - suppressWarnings(p + geom_sir() + scale_sir_colours() + labels_sir_count() + facet_sir() + theme_sir())) -} diff --git a/inst/tinytest/test-zzz.R b/inst/tinytest/test-zzz.R index 0bc25822..e83738d0 100644 --- a/inst/tinytest/test-zzz.R +++ b/inst/tinytest/test-zzz.R @@ -86,10 +86,14 @@ call_functions <- c( "ggplot" = "ggplot2", "labs" = "ggplot2", "layer" = "ggplot2", - "position_fill" = "ggplot2", "position_dodge2" = "ggplot2", + "position_fill" = "ggplot2", + "scale_colour_discrete" = "ggplot2", + "scale_fill_discrete" = "ggplot2", "scale_fill_manual" = "ggplot2", + "scale_x_discrete" = "ggplot2", "scale_y_continuous" = "ggplot2", + "scale_y_discrete" = "ggplot2", "theme" = "ggplot2", "theme_minimal" = "ggplot2", "unit" = "ggplot2", @@ -127,7 +131,7 @@ for (i in seq_len(length(import_functions))) { expect_true(!is.null(AMR:::import_fn(name = fn, pkg = pkg, error_on_fail = FALSE)), info = paste0("does not exist (anymore): function `", pkg, "::", fn, "()`") ) - } else { + } else if (pkg != "rstudioapi") { warning("Package '", pkg, "' does not exist anymore") } } @@ -140,3 +144,12 @@ if (AMR:::pkg_is_available("cli")) { if (AMR:::pkg_is_available("cli")) { expect_true(!is.null(cli::symbol$ellipsis)) } +if (AMR:::pkg_is_available("ggplot2")) { + # the scale_*_mic() functions rely on these + expect_true(is.function(ggplot2::scale_x_discrete()$transform)) + expect_true(is.function(ggplot2::scale_y_discrete()$transform)) + expect_true(is.function(ggplot2::scale_colour_discrete()$transform)) + expect_true(is.function(ggplot2::scale_fill_discrete()$transform)) +} + + diff --git a/man/plot.Rd b/man/plot.Rd index a84c6fc0..b7450e80 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -17,13 +17,13 @@ \alias{fortify.sir} \title{Plotting for Classes \code{sir}, \code{mic} and \code{disk}} \usage{ -scale_x_mic(keep_operators = "edges", mic_range = NULL, ...) +scale_x_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) -scale_y_mic(keep_operators = "edges", mic_range = NULL, ...) +scale_y_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) -scale_colour_mic(keep_operators = "edges", mic_range = NULL, ...) +scale_colour_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) -scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...) +scale_fill_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) \method{plot}{mic}( x, @@ -119,6 +119,8 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...) \item{mic_range}{a manual range to plot the MIC values, e.g., \code{mic_range = c(0.001, 32)}. Use \code{NA} to set no limit on one side, e.g., \code{mic_range = c(NA, 32)}.} +\item{drop}{a \link{logical} to remove intermediate MIC values, defaults to \code{FALSE}} + \item{...}{arguments passed on to methods} \item{x, object}{values created with \code{\link[=as.mic]{as.mic()}}, \code{\link[=as.disk]{as.disk()}} or \code{\link[=as.sir]{as.sir()}} (or their \verb{random_*} variants, such as \code{\link[=random_mic]{random_mic()}})}