diff --git a/DESCRIPTION b/DESCRIPTION index ab1b15bf0..1773aa1a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 2.1.1.9143 -Date: 2025-02-07 +Version: 2.1.1.9144 +Date: 2025-02-11 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by diff --git a/NAMESPACE b/NAMESPACE index 445aee39a..c97dbf123 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -321,6 +321,7 @@ export(resistance_predict) export(retrieve_wisca_parameters) export(rifamycins) export(right_join_microorganisms) +export(scale_color_mic) export(scale_colour_mic) export(scale_fill_mic) export(scale_sir_colours) diff --git a/NEWS.md b/NEWS.md index cb7e07030..e55957eba 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 2.1.1.9143 +# AMR 2.1.1.9144 *(this beta version will eventually become v3.0. We're happy to reach a new major milestone soon, which will be all about the new One Health support! Install this beta using [the instructions here](https://msberends.github.io/AMR/#latest-development-version).)* diff --git a/PythonPackage/AMR/AMR.egg-info/PKG-INFO b/PythonPackage/AMR/AMR.egg-info/PKG-INFO index f146f9511..0d11eea39 100644 --- a/PythonPackage/AMR/AMR.egg-info/PKG-INFO +++ b/PythonPackage/AMR/AMR.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.2 Name: AMR -Version: 2.1.1.9143 +Version: 2.1.1.9144 Summary: A Python wrapper for the AMR R package Home-page: https://github.com/msberends/AMR Author: Matthijs Berends diff --git a/PythonPackage/AMR/dist/AMR-2.1.1.9143-py3-none-any.whl b/PythonPackage/AMR/dist/AMR-2.1.1.9144-py3-none-any.whl similarity index 85% rename from PythonPackage/AMR/dist/AMR-2.1.1.9143-py3-none-any.whl rename to PythonPackage/AMR/dist/AMR-2.1.1.9144-py3-none-any.whl index 5e69bbaf9..54ddf209b 100644 Binary files a/PythonPackage/AMR/dist/AMR-2.1.1.9143-py3-none-any.whl and b/PythonPackage/AMR/dist/AMR-2.1.1.9144-py3-none-any.whl differ diff --git a/PythonPackage/AMR/dist/amr-2.1.1.9143.tar.gz b/PythonPackage/AMR/dist/amr-2.1.1.9143.tar.gz deleted file mode 100644 index 5fbe61593..000000000 Binary files a/PythonPackage/AMR/dist/amr-2.1.1.9143.tar.gz and /dev/null differ diff --git a/PythonPackage/AMR/dist/amr-2.1.1.9144.tar.gz b/PythonPackage/AMR/dist/amr-2.1.1.9144.tar.gz new file mode 100644 index 000000000..0035680bb Binary files /dev/null and b/PythonPackage/AMR/dist/amr-2.1.1.9144.tar.gz differ diff --git a/PythonPackage/AMR/setup.py b/PythonPackage/AMR/setup.py index 1d3ad34b7..ea74567b8 100644 --- a/PythonPackage/AMR/setup.py +++ b/PythonPackage/AMR/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name='AMR', - version='2.1.1.9143', + version='2.1.1.9144', packages=find_packages(), install_requires=[ 'rpy2', diff --git a/R/mic.R b/R/mic.R index df1ef5071..e19aa9c9d 100644 --- a/R/mic.R +++ b/R/mic.R @@ -207,7 +207,7 @@ as.mic <- function(x, na.rm = FALSE, keep_operators = "all") { # remove all after last digit x <- gsub("[^0-9]+$", "", x, perl = TRUE) # keep only one zero before dot - x <- gsub("0+[.]", "0.", x, perl = TRUE) + x <- gsub("^0+[.]", "0.", x, perl = TRUE) # starting 00 is probably 0.0 if there's no dot yet x[x %unlike% "[.]"] <- gsub("^00", "0.0", x[!x %like% "[.]"]) # remove last zeroes @@ -224,7 +224,7 @@ as.mic <- function(x, na.rm = FALSE, keep_operators = "all") { x[x.bak != "" & x == ""] <- "invalid" na_before <- x[is.na(x) | x == ""] %pm>% length() - x[!x %in% VALID_MIC_LEVELS] <- NA + x[!as.character(x) %in% VALID_MIC_LEVELS] <- NA na_after <- x[is.na(x) | x == ""] %pm>% length() if (na_before != na_after) { @@ -273,12 +273,22 @@ NA_mic_ <- set_clean_class(factor(NA, levels = VALID_MIC_LEVELS, ordered = TRUE) ) #' @rdname as.mic -#' @param mic_range a manual range to limit the MIC values, e.g., `mic_range = c(0.001, 32)`. Use `NA` to set no limit on one side, e.g., `mic_range = c(NA, 32)`. +#' @param mic_range a manual range to limit the MIC values, e.g., `mic_range = c(0.001, 32)`. Use `NA` to prevent a limit on one side, e.g., `mic_range = c(NA, 32)`. #' @export rescale_mic <- function(x, mic_range, keep_operators = "edges", as.mic = TRUE) { - meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE) + meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical", "mic"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE) + if (is.numeric(mic_range)) { + mic_range <- trimws(format(mic_range, scientific = FALSE)) + mic_range <- gsub("[.]0+$", "", mic_range) + mic_range[mic_range == "NA"] <- NA_character_ + } else if (is.mic(mic_range)) { + mic_range <- as.character(mic_range) + } stop_ifnot(all(mic_range %in% c(VALID_MIC_LEVELS, NA)), - "Values in `mic_range` must be valid MIC values. Unvalid: ", vector_and(mic_range[mic_range %in% c(VALID_MIC_LEVELS, NA)])) + "Values in `mic_range` must be valid MIC values. ", + "The allowed range is ", format(as.double(as.mic(VALID_MIC_LEVELS)[1]), scientific = FALSE), " to ", format(as.double(as.mic(VALID_MIC_LEVELS)[length(VALID_MIC_LEVELS)]), scientific = FALSE), ". ", + "Unvalid: ", vector_and(mic_range[!mic_range %in% c(VALID_MIC_LEVELS, NA)], quotes = FALSE), ".") + x <- as.mic(x) if (is.null(mic_range)) { mic_range <- c(NA, NA) @@ -297,7 +307,7 @@ rescale_mic <- function(x, mic_range, keep_operators = "edges", as.mic = TRUE) { x <- as.mic(x, keep_operators = ifelse(keep_operators == "edges", "none", keep_operators)) if (isTRUE(as.mic)) { - if (keep_operators == "edges") { + if (keep_operators == "edges" && length(x) > 1) { x[x == min(x, na.rm = TRUE)] <- paste0("<=", x[x == min(x, na.rm = TRUE)]) x[x == max(x, na.rm = TRUE)] <- paste0(">=", x[x == max(x, na.rm = TRUE)]) } @@ -342,7 +352,7 @@ as.numeric.mic <- function(x, ...) { #' @rdname as.mic #' @method droplevels mic -#' @param as.mic a [logical] to indicate whether the `mic` class should be kept - the default is `FALSE` +#' @param as.mic a [logical] to indicate whether the `mic` class should be kept - the default is `TRUE` for [rescale_mic()] and `FALSE` for [droplevels()]. When setting this to `FALSE` in [rescale_mic()], the output will have factor levels that acknowledge `mic_range`. #' @export droplevels.mic <- function(x, as.mic = FALSE, ...) { x <- as.mic(x) # make sure that currently implemented MIC levels are used diff --git a/R/plotting.R b/R/plotting.R index a81bdf880..976e2afe6 100755 --- a/R/plotting.R +++ b/R/plotting.R @@ -83,7 +83,7 @@ #' plot(some_disk_values, mo = "Escherichia coli", ab = "cipro", language = "nl") #' #' -#' # Plotting using scale_x_mic() --------------------------------------------- +#' # Plotting using scale_x_mic() ----------------------------------------- #' \donttest{ #' if (require("ggplot2")) { #' mic_plot <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), @@ -114,6 +114,37 @@ #' labs(title = "with scale_x_mic() using a manual 'outside' range") #' } #' +#' # Plotting using scale_y_mic() ----------------------------------------- +#' some_groups <- sample(LETTERS[1:5], 20, replace = TRUE) +#' if (require("ggplot2")) { +#' ggplot(data.frame(mic = some_mic_values, +#' group = some_groups), +#' aes(group, mic)) + +#' geom_boxplot() + +#' geom_violin(linetype = 2, colour = "grey", fill = NA) + +#' scale_y_mic() +#' } +#' if (require("ggplot2")) { +#' ggplot(data.frame(mic = some_mic_values, +#' group = some_groups), +#' aes(group, mic)) + +#' geom_boxplot() + +#' geom_violin(linetype = 2, colour = "grey", fill = NA) + +#' scale_y_mic(mic_range = c(NA, 2)) +#' } +#' +#' # Plotting using scale_fill_mic() ----------------------------------------- +#' some_counts <- as.integer(runif(20, 5, 50)) +#' if (require("ggplot2")) { +#' ggplot(data.frame(mic = some_mic_values, +#' group = some_groups, +#' counts = some_counts, +#' aes(group, counts, fill = mic)) + +#' geom_col() + +#' scale_fill_mic(mic_range = c(0.5, 16)) +#' } +#' +#' # Auto plotting -------------------------------------------------------- #' if (require("ggplot2")) { #' autoplot(some_mic_values) #' } @@ -124,7 +155,7 @@ #' autoplot(some_sir_values) #' } #' -#' # Plotting using scale_y_percent() ----------------------------------------- +#' # Plotting using scale_y_percent() ------------------------------------- #' if (require("ggplot2")) { #' p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), #' counts = c(1, 1, 2, 2, 3, 3)), @@ -145,16 +176,35 @@ #' } 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, drop = FALSE, ...) { - stop_ifnot_installed("ggplot2") - 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) +create_scale_mic <- function(aest, keep_operators, mic_range, ...) { + ggplot_fn <- getExportedValue(paste0("scale_", aest, "_continuous"), + ns = asNamespace("ggplot2")) + args <- list(...) + args[c("trans", "transform", "transform_df", "breaks", "labels", "limits")] <- NULL + scale <- do.call(ggplot_fn, args) + + scale$transform <- function(x) { + as.double(rescale_mic(x = as.double(x), keep_operators = , "labels", mic_range = mic_range, as.mic = TRUE)) + } + scale$transform_df <- function(self, df) { + self$`.values_rescaled` <- rescale_mic(x = as.double(df[[aest]]), keep_operators = keep_operators, mic_range = mic_range, as.mic = TRUE) + self$`.values_levels` <- levels(rescale_mic(x = as.double(df[[aest]]), keep_operators = keep_operators, mic_range = mic_range, as.mic = FALSE)) + if (length(self$`.values_levels`) > 6 & "0.025" %in% self$`.values_levels`) { + # TODO weird levelling out leading to 0.025 being redundant + self$`.values_levels` <- self$`.values_levels`[self$`.values_levels` != "0.025"] + } + self$`.values_log` <- log2(as.double(self$`.values_rescaled`)) + if (aest == "y" && "group" %in% colnames(df)) { + df$group <- as.integer(factor(df$x)) + } + df[[aest]] <- self$`.values_log` + df + } + scale$breaks <- function(..., self) log2(as.mic(self$`.values_levels`)) + scale$labels <- function(..., self) self$`.values_levels` + scale$limits <- function(..., self) { + rng <- range(log2(as.mic(self$`.values_levels`))) + c(rng[1] - 0.5, rng[2] + 0.5) } scale } @@ -162,40 +212,34 @@ scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE #' @export #' @inheritParams as.mic #' @rdname plot -scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { - stop_ifnot_installed("ggplot2") - 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) - } - scale +scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { + create_scale_mic("x", keep_operators = keep_operators, mic_range = mic_range, ...) } #' @export #' @inheritParams as.mic #' @rdname plot -scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { - stop_ifnot_installed("ggplot2") - 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) - } - scale +scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { + create_scale_mic("y", keep_operators = keep_operators, mic_range = mic_range, ...) } #' @export #' @inheritParams as.mic #' @rdname plot -scale_fill_mic <- function(keep_operators = "edges", mic_range = NULL, drop = FALSE, ...) { - stop_ifnot_installed("ggplot2") - 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) - } - scale +scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { + create_scale_mic("colour", keep_operators = keep_operators, mic_range = mic_range, ...) +} + +#' @export +#' @inheritParams as.mic +#' @rdname plot +scale_color_mic <- scale_colour_mic + +#' @export +#' @inheritParams as.mic +#' @rdname plot +scale_fill_mic <- function(keep_operators = "edges", mic_range = NULL, ...) { + create_scale_mic("fill", keep_operators = keep_operators, mic_range = mic_range, ...) } #' @method plot mic diff --git a/data-raw/gpt_training_text_v2.1.1.9143.txt b/data-raw/gpt_training_text_v2.1.1.9144.txt similarity index 99% rename from data-raw/gpt_training_text_v2.1.1.9143.txt rename to data-raw/gpt_training_text_v2.1.1.9144.txt index 35564233e..d972d27e1 100644 --- a/data-raw/gpt_training_text_v2.1.1.9143.txt +++ b/data-raw/gpt_training_text_v2.1.1.9144.txt @@ -1,6 +1,6 @@ This knowledge base contains all context you must know about the AMR package for R. You are a GPT trained to be an assistant for the AMR package in R. You are an incredible R specialist, especially trained in this package and in the tidyverse. -First and foremost, you are trained on version 2.1.1.9143. Remember this whenever someone asks which AMR package version you’re at. +First and foremost, you are trained on version 2.1.1.9144. Remember this whenever someone asks which AMR package version you’re at. Below are the contents of the file, the file, and all the files (documentation) in the package. Every file content is split using 100 hypens. ---------------------------------------------------------------------------------------------------- @@ -332,6 +332,7 @@ export(resistance_predict) export(retrieve_wisca_parameters) export(rifamycins) export(right_join_microorganisms) +export(scale_color_mic) export(scale_colour_mic) export(scale_fill_mic) export(scale_sir_colours) @@ -2875,9 +2876,9 @@ rescale_mic(x, mic_range, keep_operators = "edges", as.mic = TRUE) \item{keep_operators}{a \link{character} specifying how to handle operators (such as \code{>} and \code{<=}) in the input. Accepts one of three values: \code{"all"} (or \code{TRUE}) to keep all operators, \code{"none"} (or \code{FALSE}) to remove all operators, or \code{"edges"} to keep operators only at both ends of the range.} -\item{mic_range}{a manual range to limit 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{mic_range}{a manual range to limit the MIC values, e.g., \code{mic_range = c(0.001, 32)}. Use \code{NA} to prevent a limit on one side, e.g., \code{mic_range = c(NA, 32)}.} -\item{as.mic}{a \link{logical} to indicate whether the \code{mic} class should be kept - the default is \code{FALSE}} +\item{as.mic}{a \link{logical} to indicate whether the \code{mic} class should be kept - the default is \code{TRUE} for \code{\link[=rescale_mic]{rescale_mic()}} and \code{FALSE} for \code{\link[=droplevels]{droplevels()}}. When setting this to \code{FALSE} in \code{\link[=rescale_mic]{rescale_mic()}}, the output will have factor levels that acknowledge \code{mic_range}.} \item{...}{arguments passed on to methods} } @@ -7330,6 +7331,7 @@ THE PART HEREAFTER CONTAINS CONTENTS FROM FILE 'man/plot.Rd': \alias{scale_x_mic} \alias{scale_y_mic} \alias{scale_colour_mic} +\alias{scale_color_mic} \alias{scale_fill_mic} \alias{plot.mic} \alias{autoplot.mic} @@ -7347,17 +7349,15 @@ THE PART HEREAFTER CONTAINS CONTENTS FROM FILE 'man/plot.Rd': \alias{labels_sir_count} \title{Plotting Helpers for AMR Data Analysis} \usage{ -scale_x_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_x_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_y_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_y_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_colour_mic(keep_operators = "edges", mic_range = NULL, - drop = FALSE, ...) +scale_colour_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_fill_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_color_mic(keep_operators = "edges", mic_range = NULL, ...) + +scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...) \method{plot}{mic}(x, mo = NULL, ab = NULL, guideline = "EUCAST", main = deparse(substitute(x)), ylab = translate_AMR("Frequency", language @@ -7428,9 +7428,7 @@ labels_sir_count(position = NULL, x = "antibiotic", \arguments{ \item{keep_operators}{a \link{character} specifying how to handle operators (such as \code{>} and \code{<=}) in the input. Accepts one of three values: \code{"all"} (or \code{TRUE}) to keep all operators, \code{"none"} (or \code{FALSE}) to remove all operators, or \code{"edges"} to keep operators only at both ends of the range.} -\item{mic_range}{a manual range to limit 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{mic_range}{a manual range to limit the MIC values, e.g., \code{mic_range = c(0.001, 32)}. Use \code{NA} to prevent a limit on one side, e.g., \code{mic_range = c(NA, 32)}.} \item{...}{arguments passed on to methods} @@ -7521,7 +7519,7 @@ plot(some_disk_values, mo = "Escherichia coli", ab = "cipro") plot(some_disk_values, mo = "Escherichia coli", ab = "cipro", language = "nl") -# Plotting using scale_x_mic() --------------------------------------------- +# Plotting using scale_x_mic() ----------------------------------------- \donttest{ if (require("ggplot2")) { mic_plot <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), @@ -7552,6 +7550,37 @@ if (require("ggplot2")) { labs(title = "with scale_x_mic() using a manual 'outside' range") } +# Plotting using scale_y_mic() ----------------------------------------- +some_groups <- sample(LETTERS[1:5], 20, replace = TRUE) +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups), + aes(group, mic)) + + geom_boxplot() + + geom_violin(linetype = 2, colour = "grey", fill = NA) + + scale_y_mic() +} +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups), + aes(group, mic)) + + geom_boxplot() + + geom_violin(linetype = 2, colour = "grey", fill = NA) + + scale_y_mic(mic_range = c(NA, 2)) +} + +# Plotting using scale_fill_mic() ----------------------------------------- +some_counts <- as.integer(runif(20, 5, 50)) +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups, + counts = some_counts, + aes(group, counts, fill = mic)) + + geom_col() + + scale_fill_mic(mic_range = c(0.5, 16)) +} + +# Auto plotting -------------------------------------------------------- if (require("ggplot2")) { autoplot(some_mic_values) } @@ -7562,7 +7591,7 @@ if (require("ggplot2")) { autoplot(some_sir_values) } -# Plotting using scale_y_percent() ----------------------------------------- +# Plotting using scale_y_percent() ------------------------------------- if (require("ggplot2")) { p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), counts = c(1, 1, 2, 2, 3, 3)), diff --git a/man/as.mic.Rd b/man/as.mic.Rd index 786148f06..4567b1757 100644 --- a/man/as.mic.Rd +++ b/man/as.mic.Rd @@ -27,9 +27,9 @@ rescale_mic(x, mic_range, keep_operators = "edges", as.mic = TRUE) \item{keep_operators}{a \link{character} specifying how to handle operators (such as \code{>} and \code{<=}) in the input. Accepts one of three values: \code{"all"} (or \code{TRUE}) to keep all operators, \code{"none"} (or \code{FALSE}) to remove all operators, or \code{"edges"} to keep operators only at both ends of the range.} -\item{mic_range}{a manual range to limit 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{mic_range}{a manual range to limit the MIC values, e.g., \code{mic_range = c(0.001, 32)}. Use \code{NA} to prevent a limit on one side, e.g., \code{mic_range = c(NA, 32)}.} -\item{as.mic}{a \link{logical} to indicate whether the \code{mic} class should be kept - the default is \code{FALSE}} +\item{as.mic}{a \link{logical} to indicate whether the \code{mic} class should be kept - the default is \code{TRUE} for \code{\link[=rescale_mic]{rescale_mic()}} and \code{FALSE} for \code{\link[=droplevels]{droplevels()}}. When setting this to \code{FALSE} in \code{\link[=rescale_mic]{rescale_mic()}}, the output will have factor levels that acknowledge \code{mic_range}.} \item{...}{arguments passed on to methods} } diff --git a/man/plot.Rd b/man/plot.Rd index 55f2c90ff..cd6670110 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -5,6 +5,7 @@ \alias{scale_x_mic} \alias{scale_y_mic} \alias{scale_colour_mic} +\alias{scale_color_mic} \alias{scale_fill_mic} \alias{plot.mic} \alias{autoplot.mic} @@ -22,17 +23,15 @@ \alias{labels_sir_count} \title{Plotting Helpers for AMR Data Analysis} \usage{ -scale_x_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_x_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_y_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_y_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_colour_mic(keep_operators = "edges", mic_range = NULL, - drop = FALSE, ...) +scale_colour_mic(keep_operators = "edges", mic_range = NULL, ...) -scale_fill_mic(keep_operators = "edges", mic_range = NULL, drop = FALSE, - ...) +scale_color_mic(keep_operators = "edges", mic_range = NULL, ...) + +scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...) \method{plot}{mic}(x, mo = NULL, ab = NULL, guideline = "EUCAST", main = deparse(substitute(x)), ylab = translate_AMR("Frequency", language @@ -103,9 +102,7 @@ labels_sir_count(position = NULL, x = "antibiotic", \arguments{ \item{keep_operators}{a \link{character} specifying how to handle operators (such as \code{>} and \code{<=}) in the input. Accepts one of three values: \code{"all"} (or \code{TRUE}) to keep all operators, \code{"none"} (or \code{FALSE}) to remove all operators, or \code{"edges"} to keep operators only at both ends of the range.} -\item{mic_range}{a manual range to limit 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{mic_range}{a manual range to limit the MIC values, e.g., \code{mic_range = c(0.001, 32)}. Use \code{NA} to prevent a limit on one side, e.g., \code{mic_range = c(NA, 32)}.} \item{...}{arguments passed on to methods} @@ -196,7 +193,7 @@ plot(some_disk_values, mo = "Escherichia coli", ab = "cipro") plot(some_disk_values, mo = "Escherichia coli", ab = "cipro", language = "nl") -# Plotting using scale_x_mic() --------------------------------------------- +# Plotting using scale_x_mic() ----------------------------------------- \donttest{ if (require("ggplot2")) { mic_plot <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), @@ -227,6 +224,37 @@ if (require("ggplot2")) { labs(title = "with scale_x_mic() using a manual 'outside' range") } +# Plotting using scale_y_mic() ----------------------------------------- +some_groups <- sample(LETTERS[1:5], 20, replace = TRUE) +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups), + aes(group, mic)) + + geom_boxplot() + + geom_violin(linetype = 2, colour = "grey", fill = NA) + + scale_y_mic() +} +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups), + aes(group, mic)) + + geom_boxplot() + + geom_violin(linetype = 2, colour = "grey", fill = NA) + + scale_y_mic(mic_range = c(NA, 2)) +} + +# Plotting using scale_fill_mic() ----------------------------------------- +some_counts <- as.integer(runif(20, 5, 50)) +if (require("ggplot2")) { + ggplot(data.frame(mic = some_mic_values, + group = some_groups, + counts = some_counts, + aes(group, counts, fill = mic)) + + geom_col() + + scale_fill_mic(mic_range = c(0.5, 16)) +} + +# Auto plotting -------------------------------------------------------- if (require("ggplot2")) { autoplot(some_mic_values) } @@ -237,7 +265,7 @@ if (require("ggplot2")) { autoplot(some_sir_values) } -# Plotting using scale_y_percent() ----------------------------------------- +# Plotting using scale_y_percent() ------------------------------------- if (require("ggplot2")) { p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), counts = c(1, 1, 2, 2, 3, 3)),