fix scale functions

This commit is contained in:
dr. M.S. (Matthijs) Berends 2023-12-03 16:51:54 +01:00
parent c7461766ce
commit 7059568581
6 changed files with 41 additions and 51 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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)
}

View File

@ -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()))
}

View File

@ -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))
}

View File

@ -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()}})}