1
0
mirror of https://github.com/msberends/AMR.git synced 2025-02-21 20:20:09 +01:00

(v2.1.1.9146) new scale_*_sir() functions

This commit is contained in:
dr. M.S. (Matthijs) Berends 2025-02-13 19:47:57 +01:00
parent 5ff9210c12
commit bd2887bcd4
No known key found for this signature in database
16 changed files with 460 additions and 81 deletions

View File

@ -1,6 +1,6 @@
Package: AMR
Version: 2.1.1.9144
Date: 2025-02-11
Version: 2.1.1.9146
Date: 2025-02-13
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

View File

@ -323,9 +323,12 @@ export(rifamycins)
export(right_join_microorganisms)
export(scale_color_mic)
export(scale_colour_mic)
export(scale_colour_sir)
export(scale_fill_mic)
export(scale_fill_sir)
export(scale_sir_colours)
export(scale_x_mic)
export(scale_x_sir)
export(scale_y_mic)
export(scale_y_percent)
export(semi_join_microorganisms)

View File

@ -1,4 +1,4 @@
# AMR 2.1.1.9144
# AMR 2.1.1.9146
*(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).)*
@ -27,8 +27,9 @@ This package now supports not only tools for AMR data analysis in clinical setti
* **Updated clinical breakpoints**
* EUCAST 2024 and CLSI 2024 are now supported, by adding all of their over 4,000 new clinical breakpoints to the `clinical_breakpoints` data set for usage in `as.sir()`. EUCAST 2024 is now the new default guideline for all MIC and disk diffusion interpretations.
* `as.sir()` now brings additional factor levels: "NI" for non-interpretable and "SDD" for susceptible dose-dependent. Currently, the `clinical_breakpoints` data set contains 24 breakpoints that can return the value "SDD" instead of "I".
* **New forms for MIC plotting and transforming**
* New 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 easy plotting of MIC values. They allow for manual range definition and plotting missing intermediate log2 levels.
* **New advanced ggplot2 extensions for MIC and SIR plotting and transforming**
* New function group `scale_*_mic()`, namely: `scale_x_mic()`, `scale_y_mic()`, `scale_colour_mic()` and `scale_fill_mic()`. They allow easy plotting of MIC values. They allow for manual range definition and plotting missing intermediate log2 levels.
* New function group `scale_*_sir()`, namely: `scale_x_sir()`, `scale_colour_sir()` and `scale_fill_sir()`. They allow to plot the `sir` class, and translates into the system language at default. They also set colourblind-safe colours to the plots.
* New function `rescale_mic()`, which allows users to rescale MIC values to a manually set range. This is the powerhouse behind the `scale_*_mic()` functions, but it can be used independently to, for instance, compare equality in MIC distributions by rescaling them to the same range first.
* **Support for Python**
* While using R for the heavy lifting, [our 'AMR' Python Package](https://pypi.org/project/AMR/) was developed to run the AMR R package natively in Python. The Python package will always have the same version number as the R package, as it is built automatically with every code change.

View File

@ -1,6 +1,6 @@
Metadata-Version: 2.2
Name: AMR
Version: 2.1.1.9144
Version: 2.1.1.9146
Summary: A Python wrapper for the AMR R package
Home-page: https://github.com/msberends/AMR
Author: Matthijs Berends

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name='AMR',
version='2.1.1.9144',
version='2.1.1.9146',
packages=find_packages(),
install_requires=[
'rpy2',

View File

@ -77,7 +77,7 @@
#' ggplot(df) +
#' geom_sir() +
#' scale_y_percent() +
#' scale_sir_colours() +
#' scale_sir_colours(aesthetics = "fill") +
#' labels_sir_count() +
#' theme_sir()
#' }
@ -121,7 +121,8 @@
#' ) %>%
#' ggplot() +
#' geom_col(aes(x = x, y = y, fill = z)) +
#' scale_sir_colours(Value4 = "S", Value5 = "I", Value6 = "R")
#' scale_sir_colours(aesthetics = "fill",
#' Value4 = "S", Value5 = "I", Value6 = "R")
#' }
#' if (require("ggplot2") && require("dplyr")) {
#' # resistance of ciprofloxacine per age group
@ -243,7 +244,7 @@ ggplot_sir <- function(data,
theme_sir()
if (fill == "interpretation") {
p <- p + scale_sir_colours(colours = colours)
p <- p + scale_sir_colours(aesthetics = "fill", colours = colours)
}
if (identical(position, "fill")) {

View File

@ -40,38 +40,46 @@
#' @param main,title title of the plot
#' @param xlab,ylab axis title
#' @param colours_SIR colours to use for filling in the bars, must be a vector of three values (in the order S, I and R). The default colours are colour-blind friendly.
#' @param language language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see [get_AMR_locale()]) and can be overwritten by setting the package option [`AMR_locale`][AMR-options], e.g. `options(AMR_locale = "de")`, see [translate]. Use `language = NULL` or `language = ""` to prevent translation.
#' @param language language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see [get_AMR_locale()]) and can be overwritten by setting the package option [`AMR_locale`][AMR-options], e.g. `options(AMR_locale = "de")`, see [translate]. Use `language = NULL` to prevent translation.
#' @param expand a [logical] to indicate whether the range on the x axis should be expanded between the lowest and highest value. For MIC values, intermediate values will be factors of 2 starting from the highest MIC value. For disk diameters, the whole diameter range will be filled.
#' @param aesthetics aesthetics to apply the colours to - the default is "fill" but can also be (a combination of) "alpha", "colour", "fill", "linetype", "shape" or "size"
#' @param eucast_I a [logical] to indicate whether the 'I' must be interpreted as "Susceptible, under increased exposure". Will be `TRUE` if the default [AMR interpretation guideline][as.sir()] is set to EUCAST (which is the default). With `FALSE`, it will be interpreted as "Intermediate".
#' @inheritParams as.sir
#' @inheritParams ggplot_sir
#' @inheritParams proportion
#' @details
#' The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
#'
#' For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the `guideline` argument are: `r vector_and(AMR::clinical_breakpoints$guideline, quotes = TRUE, reverse = TRUE)`.
#'
#' Simply using `"CLSI"` or `"EUCAST"` as input will automatically select the latest version of that guideline.
#' ### The `scale_*_mic()` Functions
#'
#' The functions [scale_x_mic()], [scale_y_mic()], [scale_colour_mic()], and [scale_fill_mic()] functions allow to plot the [mic][as.mic()] class (MIC values) on a continuous scale. They allow to rescale the MIC range, and retain the signs in MIC values if desired. Missing intermediate log2 levels will be plotted too.
#'
#' ### The `scale_*_sir()` Functions
#'
#' The functions [scale_x_sir()], [scale_colour_sir()], and [scale_fill_sir()] functions allow to plot the [sir][as.sir()] class (S/I/R values). They can translate the S/I/R values to any of the `r length(AMR:::LANGUAGES_SUPPORTED)` supported languages, and set colour-blind friendly colours to the `colour` and `fill` aesthetics.
#'
#' ### Additional `ggplot2` Functions
#'
#' This package contains several functions that extend the `ggplot2` package, to help in visualising AMR data results. All these functions are internally used by [ggplot_sir()] too.
#' This package contains more functions that extend the `ggplot2` package, to help in visualising AMR data results. All these functions are internally used by [ggplot_sir()] too.
#'
#' * [facet_sir()] creates 2d plots (at default based on S/I/R) using [ggplot2::facet_wrap()].
#' * [scale_y_percent()] transforms the y axis to a 0 to 100% range using [ggplot2::scale_y_continuous()].
#' * [scale_sir_colours()] sets colours to the bars (green for S, yellow for I, and red for R). Has multilingual support. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red.
#' * [scale_sir_colours()] allows to set colours to any aesthetic, even for `shape` or `linetype`.
#' * [theme_sir()] is a [ggplot2 theme][[ggplot2::theme()] with minimal distraction.
#' * [labels_sir_count()] print datalabels on the bars with percentage and number of isolates, using [ggplot2::geom_text()].
#'
#' The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
#'
#' For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the `guideline` argument are: `r vector_and(AMR::clinical_breakpoints$guideline, quotes = TRUE, reverse = TRUE)`. Simply using `"CLSI"` or `"EUCAST"` as input will automatically select the latest version of that guideline.
#' @name plot
#' @rdname plot
#' @return The `autoplot()` functions return a [`ggplot`][ggplot2::ggplot()] model that is extendible with any `ggplot2` function.
#'
#' The `fortify()` functions return a [data.frame] as an extension for usage in the [ggplot2::ggplot()] function.
#' @param ... arguments passed on to methods
#' @examples
#' some_mic_values <- random_mic(size = 100)
#' some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro")
#' some_sir_values <- random_sir(50, prob_SIR = c(0.55, 0.05, 0.30))
#'
#'
#' # Plotting using base R's plot() ---------------------------------------
#'
#' plot(some_mic_values)
#' plot(some_disk_values)
@ -114,8 +122,10 @@
#' 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),
@ -133,18 +143,58 @@
#' 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,
#' counts = some_counts),
#' aes(group, counts, fill = mic)) +
#' geom_col() +
#' scale_fill_mic(mic_range = c(0.5, 16))
#' }
#'
#' # Auto plotting --------------------------------------------------------
#' # Plotting using scale_x_sir() -----------------------------------------
#' if (require("ggplot2")) {
#' ggplot(data.frame(x = c("I", "R", "S"),
#' y = c(45,323, 573)),
#' aes(x, y)) +
#' geom_col() +
#' scale_x_sir()
#' }
#'
#'
#' # Plotting using scale_y_mic() and scale_colour_sir() ------------------
#' if (require("ggplot2")) {
#' plain <- ggplot(data.frame(mic = some_mic_values,
#' group = some_groups,
#' sir = as.sir(some_mic_values,
#' mo = "E. coli",
#' ab = "cipro")),
#' aes(x = group, y = mic, colour = sir)) +
#' theme_minimal() +
#' geom_boxplot(fill = NA, colour = "grey") +
#' geom_jitter(width = 0.25)
#'
#' plain
#' }
#' if (require("ggplot2")) {
#' # and now with our MIC and SIR scale functions:
#' plain +
#' scale_y_mic() +
#' scale_colour_sir()
#' }
#' if (require("ggplot2")) {
#' plain +
#' scale_y_mic(mic_range = c(0.005, 32), name = "Our MICs!") +
#' scale_colour_sir(language = "el", eucast_I = FALSE,
#' name = "In Greek!")
#' }
#'
#'
#' # Plotting using ggplot2's autoplot() ----------------------------------
#' if (require("ggplot2")) {
#' autoplot(some_mic_values)
#' }
@ -155,6 +205,7 @@
#' autoplot(some_sir_values)
#' }
#'
#'
#' # Plotting using scale_y_percent() -------------------------------------
#' if (require("ggplot2")) {
#' p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")),
@ -202,9 +253,15 @@ create_scale_mic <- function(aest, keep_operators, mic_range, ...) {
}
scale$breaks <- function(..., self) log2(as.mic(self$`.values_levels`))
scale$labels <- function(..., self) self$`.values_levels`
scale$limits <- function(..., self) {
scale$limits <- function(x, ..., self) {
rng <- range(log2(as.mic(self$`.values_levels`)))
c(rng[1] - 0.5, rng[2] + 0.5)
# add 0.5 extra space
rng <- c(rng[1] - 0.5, rng[2] + 0.5)
if (!is.na(x[1]) && x[1] == 0) {
# scale that start at 0 must remain so, e.g. in case of geom_col()
rng[1] <- 0
}
rng
}
scale
}
@ -213,6 +270,8 @@ create_scale_mic <- function(aest, keep_operators, mic_range, ...) {
#' @inheritParams as.mic
#' @rdname plot
scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
meet_criteria(keep_operators, allow_class = c("character", "logical"), is_in = c("all", "none", "edges", FALSE, TRUE), has_length = 1)
meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical", "mic"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE)
create_scale_mic("x", keep_operators = keep_operators, mic_range = mic_range, ...)
}
@ -220,6 +279,8 @@ scale_x_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
#' @inheritParams as.mic
#' @rdname plot
scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
meet_criteria(keep_operators, allow_class = c("character", "logical"), is_in = c("all", "none", "edges", FALSE, TRUE), has_length = 1)
meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical", "mic"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE)
create_scale_mic("y", keep_operators = keep_operators, mic_range = mic_range, ...)
}
@ -227,6 +288,8 @@ scale_y_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
#' @inheritParams as.mic
#' @rdname plot
scale_colour_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
meet_criteria(keep_operators, allow_class = c("character", "logical"), is_in = c("all", "none", "edges", FALSE, TRUE), has_length = 1)
meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical", "mic"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE)
create_scale_mic("colour", keep_operators = keep_operators, mic_range = mic_range, ...)
}
@ -239,9 +302,97 @@ scale_color_mic <- scale_colour_mic
#' @inheritParams as.mic
#' @rdname plot
scale_fill_mic <- function(keep_operators = "edges", mic_range = NULL, ...) {
meet_criteria(keep_operators, allow_class = c("character", "logical"), is_in = c("all", "none", "edges", FALSE, TRUE), has_length = 1)
meet_criteria(mic_range, allow_class = c("numeric", "integer", "logical", "mic"), has_length = 2, allow_NA = TRUE, allow_NULL = TRUE)
create_scale_mic("fill", keep_operators = keep_operators, mic_range = mic_range, ...)
}
create_scale_sir <- function(aest, colours_SIR, language, eucast_I, ...) {
ggplot_fn <- getExportedValue(paste0("scale_", aest, "_discrete"),
ns = asNamespace("ggplot2"))
args <- list(...)
args[c("aesthetics", "value", "labels", "limits")] <- NULL
if (aest == "x") {
ggplot_fn <- ggplot2::scale_x_discrete
args <- c(args,
list(limits = base::force))
} else {
ggplot_fn <- ggplot2::scale_discrete_manual
args <- c(args,
list(aesthetics = aest,
values = c(S = colours_SIR[1],
SDD = colours_SIR[2],
I = colours_SIR[2],
R = colours_SIR[3],
NI = "grey30"),
limits = base::force))
}
scale <- do.call(ggplot_fn, args)
scale$labels <- function(x) {
stop_ifnot(all(x %in% levels(NA_sir_)),
"Apply `scale_", aest, "_sir()` to a variable of class 'sir', see `?as.sir`.",
call = FALSE)
x <- as.character(as.sir(x))
if (!is.null(language)) {
x[x == "S"] <- "(S) Susceptible"
x[x == "SDD"] <- "(SDD) Susceptible dose-dependent"
if (eucast_I == TRUE) {
x[x == "I"] <- "(I) Susceptible, incr. exp."
} else {
x[x == "I"] <- "(I) Intermediate"
}
x[x == "R"] <- "(R) Resistant"
x[x == "NI"] <- "(NI) Not interpretable"
x <- translate_AMR(x, language = language)
}
x
}
scale$limits <- function(x, ...) {
# force SIR in the right order
x[match(x, levels(NA_sir_))]
}
scale
}
#' @rdname plot
#' @export
scale_x_sir <- function(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(),
eucast_I = getOption("AMR_guideline", "EUCAST") == "EUCAST",
...) {
meet_criteria(colours_SIR, allow_class = "character", has_length = c(1, 3))
language <- validate_language(language)
meet_criteria(eucast_I, allow_class = "logical", has_length = 1)
create_scale_sir("x", colours_SIR = colours_SIR, language = language, eucast_I = eucast_I, ...)
}
#' @rdname plot
#' @export
scale_colour_sir <- function(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(),
eucast_I = getOption("AMR_guideline", "EUCAST") == "EUCAST",
...) {
meet_criteria(colours_SIR, allow_class = "character", has_length = c(1, 3))
language <- validate_language(language)
meet_criteria(eucast_I, allow_class = "logical", has_length = 1)
create_scale_sir("colour", colours_SIR = colours_SIR, language = language, eucast_I = eucast_I, ...)
}
#' @rdname plot
#' @export
scale_fill_sir <- function(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(),
eucast_I = getOption("AMR_guideline", "EUCAST") == "EUCAST",
...) {
meet_criteria(colours_SIR, allow_class = "character", has_length = c(1, 3))
language <- validate_language(language)
meet_criteria(eucast_I, allow_class = "logical", has_length = 1)
create_scale_sir("fill", colours_SIR = colours_SIR, language = language, eucast_I = eucast_I, ...)
}
#' @method plot mic
#' @importFrom graphics barplot axis mtext legend
#' @export
@ -468,7 +619,7 @@ autoplot.mic <- function(object,
}
#' @method fortify mic
#' @rdname plot
#' @noRd
# will be exported using s3_register() in R/zzz.R
fortify.mic <- function(object, ...) {
object <- as.mic(object) # make sure that currently implemented MIC levels are used
@ -701,7 +852,7 @@ autoplot.disk <- function(object,
}
#' @method fortify disk
#' @rdname plot
#' @noRd
# will be exported using s3_register() in R/zzz.R
fortify.disk <- function(object, ...) {
stats::setNames(
@ -861,7 +1012,7 @@ autoplot.sir <- function(object,
}
#' @method fortify sir
#' @rdname plot
#' @noRd
# will be exported using s3_register() in R/zzz.R
fortify.sir <- function(object, ...) {
stats::setNames(
@ -1037,11 +1188,19 @@ scale_y_percent <- function(breaks = function(x) seq(0, max(x, na.rm = TRUE), 0.
#' @rdname plot
#' @export
scale_sir_colours <- function(...,
aesthetics = "fill",
aesthetics,
colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B")) {
stop_ifnot_installed("ggplot2")
meet_criteria(aesthetics, allow_class = "character", is_in = c("alpha", "colour", "color", "fill", "linetype", "shape", "size"))
meet_criteria(colours_SIR, allow_class = "character", has_length = c(1, 3))
if ("fill" %in% aesthetics && message_not_thrown_before("scale_sir_colours", "fill", entire_session = TRUE)) {
warning_("Using `scale_sir_colours()` for the `fill` aesthetic has been superseded by `scale_fill_sir()`, please use that instead. This warning will be shown once per session.")
}
if (any(c("colour", "color") %in% aesthetics) && message_not_thrown_before("scale_sir_colours", "colour", entire_session = TRUE)) {
warning_("Using `scale_sir_colours()` for the `colour` aesthetic has been superseded by `scale_colour_sir()`, please use that instead. This warning will be shown once per session.")
}
if (length(colours_SIR) == 1) {
colours_SIR <- rep(colours_SIR, 3)
}
@ -1057,7 +1216,7 @@ scale_sir_colours <- function(...,
colours <- replace(original_cols, names(list(...)$colours), list(...)$colours)
# limits = force is needed in ggplot2 3.3.4 and 3.3.5, see here;
# https://github.com/tidyverse/ggplot2/issues/4511#issuecomment-866185530
return(ggplot2::scale_fill_manual(values = colours, limits = force))
return(ggplot2::scale_fill_manual(values = colours, limits = force, aesthetics = aesthetics))
}
if (identical(unlist(list(...)), FALSE)) {
return(invisible())

Binary file not shown.

View File

@ -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.9144. Remember this whenever someone asks which AMR package version youre at.
First and foremost, you are trained on version 2.1.1.9146. Remember this whenever someone asks which AMR package version youre 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.
----------------------------------------------------------------------------------------------------
@ -334,9 +334,12 @@ export(rifamycins)
export(right_join_microorganisms)
export(scale_color_mic)
export(scale_colour_mic)
export(scale_colour_sir)
export(scale_fill_mic)
export(scale_fill_sir)
export(scale_sir_colours)
export(scale_x_mic)
export(scale_x_sir)
export(scale_y_mic)
export(scale_y_percent)
export(semi_join_microorganisms)
@ -5561,7 +5564,7 @@ if (require("ggplot2") && require("dplyr")) {
ggplot(df) +
geom_sir() +
scale_y_percent() +
scale_sir_colours() +
scale_sir_colours(aesthetics = "fill") +
labels_sir_count() +
theme_sir()
}
@ -5605,7 +5608,8 @@ if (require("ggplot2") && require("dplyr")) {
) \%>\%
ggplot() +
geom_col(aes(x = x, y = y, fill = z)) +
scale_sir_colours(Value4 = "S", Value5 = "I", Value6 = "R")
scale_sir_colours(aesthetics = "fill",
Value4 = "S", Value5 = "I", Value6 = "R")
}
if (require("ggplot2") && require("dplyr")) {
# resistance of ciprofloxacine per age group
@ -7333,15 +7337,15 @@ THE PART HEREAFTER CONTAINS CONTENTS FROM FILE 'man/plot.Rd':
\alias{scale_colour_mic}
\alias{scale_color_mic}
\alias{scale_fill_mic}
\alias{scale_x_sir}
\alias{scale_colour_sir}
\alias{scale_fill_sir}
\alias{plot.mic}
\alias{autoplot.mic}
\alias{fortify.mic}
\alias{plot.disk}
\alias{autoplot.disk}
\alias{fortify.disk}
\alias{plot.sir}
\alias{autoplot.sir}
\alias{fortify.sir}
\alias{facet_sir}
\alias{scale_y_percent}
\alias{scale_sir_colours}
@ -7359,6 +7363,18 @@ scale_color_mic(keep_operators = "edges", mic_range = NULL, ...)
scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
scale_x_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
scale_colour_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
scale_fill_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
\method{plot}{mic}(x, mo = NULL, ab = NULL, guideline = "EUCAST",
main = deparse(substitute(x)), ylab = translate_AMR("Frequency", language
= language),
@ -7377,8 +7393,6 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
include_PKPD = getOption("AMR_include_PKPD", TRUE),
breakpoint_type = getOption("AMR_breakpoint_type", "human"), ...)
\method{fortify}{mic}(object, ...)
\method{plot}{disk}(x, main = deparse(substitute(x)),
ylab = translate_AMR("Frequency", language = language),
xlab = translate_AMR("Disk diffusion diameter (mm)", language = language),
@ -7396,8 +7410,6 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
include_PKPD = getOption("AMR_include_PKPD", TRUE),
breakpoint_type = getOption("AMR_breakpoint_type", "human"), ...)
\method{fortify}{disk}(object, ...)
\method{plot}{sir}(x, ylab = translate_AMR("Percentage", language =
language), xlab = translate_AMR("Antimicrobial Interpretation", language =
language), main = deparse(substitute(x)), language = get_AMR_locale(),
@ -7409,15 +7421,13 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), ...)
\method{fortify}{sir}(object, ...)
facet_sir(facet = c("interpretation", "antibiotic"), nrow = NULL)
scale_y_percent(breaks = function(x) seq(0, max(x, na.rm = TRUE), 0.1),
limits = c(0, NA))
scale_sir_colours(..., aesthetics = "fill", colours_SIR = c("#3CAEA3",
"#F6D55C", "#ED553B"))
scale_sir_colours(..., aesthetics, colours_SIR = c("#3CAEA3", "#F6D55C",
"#ED553B"))
theme_sir()
@ -7432,6 +7442,12 @@ labels_sir_count(position = NULL, x = "antibiotic",
\item{...}{arguments passed on to methods}
\item{colours_SIR}{colours to use for filling in the bars, must be a vector of three values (in the order S, I and R). The default colours are colour-blind friendly.}
\item{language}{language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see \code{\link[=get_AMR_locale]{get_AMR_locale()}}) and can be overwritten by setting the package option \code{\link[=AMR-options]{AMR_locale}}, e.g. \code{options(AMR_locale = "de")}, see \link{translate}. Use \code{language = NULL} to prevent translation.}
\item{eucast_I}{a \link{logical} to indicate whether the 'I' must be interpreted as "Susceptible, under increased exposure". Will be \code{TRUE} if the default \link[=as.sir]{AMR interpretation guideline} is set to EUCAST (which is the default). With \code{FALSE}, it will be interpreted as "Intermediate".}
\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()}})}
\item{mo}{any (vector of) text that can be coerced to a valid microorganism code with \code{\link[=as.mo]{as.mo()}}}
@ -7444,10 +7460,6 @@ labels_sir_count(position = NULL, x = "antibiotic",
\item{xlab, ylab}{axis title}
\item{colours_SIR}{colours to use for filling in the bars, must be a vector of three values (in the order S, I and R). The default colours are colour-blind friendly.}
\item{language}{language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see \code{\link[=get_AMR_locale]{get_AMR_locale()}}) and can be overwritten by setting the package option \code{\link[=AMR-options]{AMR_locale}}, e.g. \code{options(AMR_locale = "de")}, see \link{translate}. Use \code{language = NULL} or \code{language = ""} to prevent translation.}
\item{expand}{a \link{logical} to indicate whether the range on the x axis should be expanded between the lowest and highest value. For MIC values, intermediate values will be factors of 2 starting from the highest MIC value. For disk diameters, the whole diameter range will be filled.}
\item{include_PKPD}{a \link{logical} to indicate that PK/PD clinical breakpoints must be applied as a last resort - the default is \code{TRUE}. Can also be set with the package option \code{\link[=AMR-options]{AMR_include_PKPD}}.}
@ -7478,8 +7490,6 @@ labels_sir_count(position = NULL, x = "antibiotic",
}
\value{
The \code{autoplot()} functions return a \code{\link[ggplot2:ggplot]{ggplot}} model that is extendible with any \code{ggplot2} function.
The \code{fortify()} functions return a \link{data.frame} as an extension for usage in the \code{\link[ggplot2:ggplot]{ggplot2::ggplot()}} function.
}
\description{
Functions to plot classes \code{sir}, \code{mic} and \code{disk}, with support for base \R and \code{ggplot2}.
@ -7487,21 +7497,30 @@ Functions to plot classes \code{sir}, \code{mic} and \code{disk}, with support f
Especially the \verb{scale_*_mic()} functions are relevant wrappers to plot MIC values for \code{ggplot2}. They allows custom MIC ranges and to plot intermediate log2 levels for missing MIC values.
}
\details{
The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
\subsection{The \verb{scale_*_mic()} Functions}{
For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the \code{guideline} argument are: "EUCAST 2024", "EUCAST 2023", "EUCAST 2022", "EUCAST 2021", "EUCAST 2020", "EUCAST 2019", "EUCAST 2018", "EUCAST 2017", "EUCAST 2016", "EUCAST 2015", "EUCAST 2014", "EUCAST 2013", "EUCAST 2012", "EUCAST 2011", "CLSI 2024", "CLSI 2023", "CLSI 2022", "CLSI 2021", "CLSI 2020", "CLSI 2019", "CLSI 2018", "CLSI 2017", "CLSI 2016", "CLSI 2015", "CLSI 2014", "CLSI 2013", "CLSI 2012", and "CLSI 2011".
The functions \code{\link[=scale_x_mic]{scale_x_mic()}}, \code{\link[=scale_y_mic]{scale_y_mic()}}, \code{\link[=scale_colour_mic]{scale_colour_mic()}}, and \code{\link[=scale_fill_mic]{scale_fill_mic()}} functions allow to plot the \link[=as.mic]{mic} class (MIC values) on a continuous scale. They allow to rescale the MIC range, and retain the signs in MIC values if desired. Missing intermediate log2 levels will be plotted too.
}
\subsection{The \verb{scale_*_sir()} Functions}{
The functions \code{\link[=scale_x_sir]{scale_x_sir()}}, \code{\link[=scale_colour_sir]{scale_colour_sir()}}, and \code{\link[=scale_fill_sir]{scale_fill_sir()}} functions allow to plot the \link[=as.sir]{sir} class (S/I/R values). They can translate the S/I/R values to any of the 20 supported languages, and set colour-blind friendly colours to the \code{colour} and \code{fill} aesthetics.
}
Simply using \code{"CLSI"} or \code{"EUCAST"} as input will automatically select the latest version of that guideline.
\subsection{Additional \code{ggplot2} Functions}{
This package contains several functions that extend the \code{ggplot2} package, to help in visualising AMR data results. All these functions are internally used by \code{\link[=ggplot_sir]{ggplot_sir()}} too.
This package contains more functions that extend the \code{ggplot2} package, to help in visualising AMR data results. All these functions are internally used by \code{\link[=ggplot_sir]{ggplot_sir()}} too.
\itemize{
\item \code{\link[=facet_sir]{facet_sir()}} creates 2d plots (at default based on S/I/R) using \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}}.
\item \code{\link[=scale_y_percent]{scale_y_percent()}} transforms the y axis to a 0 to 100\% range using \code{\link[ggplot2:scale_continuous]{ggplot2::scale_y_continuous()}}.
\item \code{\link[=scale_sir_colours]{scale_sir_colours()}} sets colours to the bars (green for S, yellow for I, and red for R). Has multilingual support. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red.
\item \code{\link[=scale_sir_colours]{scale_sir_colours()}} allows to set colours to any aesthetic, even for \code{shape} or \code{linetype}.
\item \code{\link[=theme_sir]{theme_sir()}} is a [ggplot2 theme][\code{\link[ggplot2:theme]{ggplot2::theme()}} with minimal distraction.
\item \code{\link[=labels_sir_count]{labels_sir_count()}} print datalabels on the bars with percentage and number of isolates, using \code{\link[ggplot2:geom_text]{ggplot2::geom_text()}}.
}
The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the \code{guideline} argument are: "EUCAST 2024", "EUCAST 2023", "EUCAST 2022", "EUCAST 2021", "EUCAST 2020", "EUCAST 2019", "EUCAST 2018", "EUCAST 2017", "EUCAST 2016", "EUCAST 2015", "EUCAST 2014", "EUCAST 2013", "EUCAST 2012", "EUCAST 2011", "CLSI 2024", "CLSI 2023", "CLSI 2022", "CLSI 2021", "CLSI 2020", "CLSI 2019", "CLSI 2018", "CLSI 2017", "CLSI 2016", "CLSI 2015", "CLSI 2014", "CLSI 2013", "CLSI 2012", and "CLSI 2011". Simply using \code{"CLSI"} or \code{"EUCAST"} as input will automatically select the latest version of that guideline.
}
}
\examples{
@ -7509,6 +7528,9 @@ some_mic_values <- random_mic(size = 100)
some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro")
some_sir_values <- random_sir(50, prob_SIR = c(0.55, 0.05, 0.30))
# Plotting using base R's plot() ---------------------------------------
plot(some_mic_values)
plot(some_disk_values)
plot(some_sir_values)
@ -7550,8 +7572,10 @@ 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),
@ -7569,18 +7593,58 @@ if (require("ggplot2")) {
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,
counts = some_counts),
aes(group, counts, fill = mic)) +
geom_col() +
scale_fill_mic(mic_range = c(0.5, 16))
}
# Auto plotting --------------------------------------------------------
# Plotting using scale_x_sir() -----------------------------------------
if (require("ggplot2")) {
ggplot(data.frame(x = c("I", "R", "S"),
y = c(45,323, 573)),
aes(x, y)) +
geom_col() +
scale_x_sir()
}
# Plotting using scale_y_mic() and scale_colour_sir() ------------------
if (require("ggplot2")) {
plain <- ggplot(data.frame(mic = some_mic_values,
group = some_groups,
sir = as.sir(some_mic_values,
mo = "E. coli",
ab = "cipro")),
aes(x = group, y = mic, colour = sir)) +
theme_minimal() +
geom_boxplot(fill = NA, colour = "grey") +
geom_jitter(width = 0.25)
plain
}
if (require("ggplot2")) {
# and now with our MIC and SIR scale functions:
plain +
scale_y_mic() +
scale_colour_sir()
}
if (require("ggplot2")) {
plain +
scale_y_mic(mic_range = c(0.005, 32), name = "Our MICs!") +
scale_colour_sir(language = "el", eucast_I = FALSE,
name = "In Greek!")
}
# Plotting using ggplot2's autoplot() ----------------------------------
if (require("ggplot2")) {
autoplot(some_mic_values)
}
@ -7591,6 +7655,7 @@ if (require("ggplot2")) {
autoplot(some_sir_values)
}
# Plotting using scale_y_percent() -------------------------------------
if (require("ggplot2")) {
p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")),

View File

@ -45,6 +45,7 @@ susceptible, incr. exp. FALSE TRUE FALSE FALSE 易感,接触增加 náchylná,
Susceptible TRUE FALSE FALSE FALSE 易受影响 Susceptible Modtagelig Gevoelig Altis Empfindlich Ευαίσθητο 影響を受けやすい Mottakelig Podatny Susceptibil Susceptible Mottaglig Duyarlı Чутливий
Incr. exposure TRUE FALSE FALSE FALSE 暴露增加 zvýšená expozice Øget eksponering 'Incr. exposure' Lisääntynyt altistuminen Empfindlich, erh Belastung Αυξημένη έκθεση 曝露量増加 Økt eksp. Większe narażenie Exp. crescută Mayor exposición Inkr. exponering Enk. maruziyet Підвищена експозиція
Resistant TRUE FALSE FALSE FALSE 耐药性 Rezistentní Resistent Resistent Kestävä Resistent Ανθεκτικός 耐性 Resistent Odporny Rezistent Resistente Resistent Dayanıklı Стійкий
Not interpretable TRUE FALSE FALSE FALSE 无法解释 Nelze interpretovat Ufortolkelig Niet interpreteerbaar Ei tulkittavissa Non interprétable Nicht interpretierbar Μη ερμηνεύσιμο Non interpretabile 解釈不可 Utolkelig ikke Niemożliwe do interpretacji Neinterpretabil Непереводимо No interpretable Inte tolkningsbar Yorumlanamaz Непридатний до інтерпретації
antibiotic TRUE TRUE FALSE FALSE 抗生素 antibiotikum antibiotikum antibioticum antibiootti antibiotique Antibiotikum αντιβιοτικό antibiotico 抗生物質 Antibiotikum antybiotyk antibiótico antibiotic антибиотик antibiótico antibiotika Antibiyotik антибіотик
Antibiotic TRUE TRUE FALSE FALSE 抗生素 Antibiotikum Antibiotikum Antibioticum Antibiootti Antibiotique Antibiotikum Αντιβιοτικό Antibiotico 抗生物質 Antibiotikum Antybiotyk Antibiótico Antibiotic Антибиотик Antibiótico Antibiotika Antibiyotik Антибіотик
Drug TRUE TRUE FALSE FALSE 药物 Lék Lægemiddel Middel Lääke Médicament Medikament Φάρμακο Droga 薬剤 Legemiddel Lek Droga Medicament Лекарство Fármaco Läkemedel İlaç Лікарський засіб

Can't render this file because it has a wrong number of fields in line 48.

View File

@ -95,7 +95,7 @@ if (require("ggplot2") && require("dplyr")) {
ggplot(df) +
geom_sir() +
scale_y_percent() +
scale_sir_colours() +
scale_sir_colours(aesthetics = "fill") +
labels_sir_count() +
theme_sir()
}
@ -139,7 +139,8 @@ if (require("ggplot2") && require("dplyr")) {
) \%>\%
ggplot() +
geom_col(aes(x = x, y = y, fill = z)) +
scale_sir_colours(Value4 = "S", Value5 = "I", Value6 = "R")
scale_sir_colours(aesthetics = "fill",
Value4 = "S", Value5 = "I", Value6 = "R")
}
if (require("ggplot2") && require("dplyr")) {
# resistance of ciprofloxacine per age group

View File

@ -7,15 +7,15 @@
\alias{scale_colour_mic}
\alias{scale_color_mic}
\alias{scale_fill_mic}
\alias{scale_x_sir}
\alias{scale_colour_sir}
\alias{scale_fill_sir}
\alias{plot.mic}
\alias{autoplot.mic}
\alias{fortify.mic}
\alias{plot.disk}
\alias{autoplot.disk}
\alias{fortify.disk}
\alias{plot.sir}
\alias{autoplot.sir}
\alias{fortify.sir}
\alias{facet_sir}
\alias{scale_y_percent}
\alias{scale_sir_colours}
@ -33,6 +33,18 @@ scale_color_mic(keep_operators = "edges", mic_range = NULL, ...)
scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
scale_x_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
scale_colour_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
scale_fill_sir(colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), eucast_I = getOption("AMR_guideline",
"EUCAST") == "EUCAST", ...)
\method{plot}{mic}(x, mo = NULL, ab = NULL, guideline = "EUCAST",
main = deparse(substitute(x)), ylab = translate_AMR("Frequency", language
= language),
@ -51,8 +63,6 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
include_PKPD = getOption("AMR_include_PKPD", TRUE),
breakpoint_type = getOption("AMR_breakpoint_type", "human"), ...)
\method{fortify}{mic}(object, ...)
\method{plot}{disk}(x, main = deparse(substitute(x)),
ylab = translate_AMR("Frequency", language = language),
xlab = translate_AMR("Disk diffusion diameter (mm)", language = language),
@ -70,8 +80,6 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
include_PKPD = getOption("AMR_include_PKPD", TRUE),
breakpoint_type = getOption("AMR_breakpoint_type", "human"), ...)
\method{fortify}{disk}(object, ...)
\method{plot}{sir}(x, ylab = translate_AMR("Percentage", language =
language), xlab = translate_AMR("Antimicrobial Interpretation", language =
language), main = deparse(substitute(x)), language = get_AMR_locale(),
@ -83,15 +91,13 @@ scale_fill_mic(keep_operators = "edges", mic_range = NULL, ...)
colours_SIR = c("#3CAEA3", "#F6D55C", "#ED553B"),
language = get_AMR_locale(), ...)
\method{fortify}{sir}(object, ...)
facet_sir(facet = c("interpretation", "antibiotic"), nrow = NULL)
scale_y_percent(breaks = function(x) seq(0, max(x, na.rm = TRUE), 0.1),
limits = c(0, NA))
scale_sir_colours(..., aesthetics = "fill", colours_SIR = c("#3CAEA3",
"#F6D55C", "#ED553B"))
scale_sir_colours(..., aesthetics, colours_SIR = c("#3CAEA3", "#F6D55C",
"#ED553B"))
theme_sir()
@ -106,6 +112,12 @@ labels_sir_count(position = NULL, x = "antibiotic",
\item{...}{arguments passed on to methods}
\item{colours_SIR}{colours to use for filling in the bars, must be a vector of three values (in the order S, I and R). The default colours are colour-blind friendly.}
\item{language}{language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see \code{\link[=get_AMR_locale]{get_AMR_locale()}}) and can be overwritten by setting the package option \code{\link[=AMR-options]{AMR_locale}}, e.g. \code{options(AMR_locale = "de")}, see \link{translate}. Use \code{language = NULL} to prevent translation.}
\item{eucast_I}{a \link{logical} to indicate whether the 'I' must be interpreted as "Susceptible, under increased exposure". Will be \code{TRUE} if the default \link[=as.sir]{AMR interpretation guideline} is set to EUCAST (which is the default). With \code{FALSE}, it will be interpreted as "Intermediate".}
\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()}})}
\item{mo}{any (vector of) text that can be coerced to a valid microorganism code with \code{\link[=as.mo]{as.mo()}}}
@ -118,10 +130,6 @@ labels_sir_count(position = NULL, x = "antibiotic",
\item{xlab, ylab}{axis title}
\item{colours_SIR}{colours to use for filling in the bars, must be a vector of three values (in the order S, I and R). The default colours are colour-blind friendly.}
\item{language}{language to be used to translate 'Susceptible', 'Increased exposure'/'Intermediate' and 'Resistant' - the default is system language (see \code{\link[=get_AMR_locale]{get_AMR_locale()}}) and can be overwritten by setting the package option \code{\link[=AMR-options]{AMR_locale}}, e.g. \code{options(AMR_locale = "de")}, see \link{translate}. Use \code{language = NULL} or \code{language = ""} to prevent translation.}
\item{expand}{a \link{logical} to indicate whether the range on the x axis should be expanded between the lowest and highest value. For MIC values, intermediate values will be factors of 2 starting from the highest MIC value. For disk diameters, the whole diameter range will be filled.}
\item{include_PKPD}{a \link{logical} to indicate that PK/PD clinical breakpoints must be applied as a last resort - the default is \code{TRUE}. Can also be set with the package option \code{\link[=AMR-options]{AMR_include_PKPD}}.}
@ -152,8 +160,6 @@ labels_sir_count(position = NULL, x = "antibiotic",
}
\value{
The \code{autoplot()} functions return a \code{\link[ggplot2:ggplot]{ggplot}} model that is extendible with any \code{ggplot2} function.
The \code{fortify()} functions return a \link{data.frame} as an extension for usage in the \code{\link[ggplot2:ggplot]{ggplot2::ggplot()}} function.
}
\description{
Functions to plot classes \code{sir}, \code{mic} and \code{disk}, with support for base \R and \code{ggplot2}.
@ -161,21 +167,30 @@ Functions to plot classes \code{sir}, \code{mic} and \code{disk}, with support f
Especially the \verb{scale_*_mic()} functions are relevant wrappers to plot MIC values for \code{ggplot2}. They allows custom MIC ranges and to plot intermediate log2 levels for missing MIC values.
}
\details{
The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
\subsection{The \verb{scale_*_mic()} Functions}{
For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the \code{guideline} argument are: "EUCAST 2024", "EUCAST 2023", "EUCAST 2022", "EUCAST 2021", "EUCAST 2020", "EUCAST 2019", "EUCAST 2018", "EUCAST 2017", "EUCAST 2016", "EUCAST 2015", "EUCAST 2014", "EUCAST 2013", "EUCAST 2012", "EUCAST 2011", "CLSI 2024", "CLSI 2023", "CLSI 2022", "CLSI 2021", "CLSI 2020", "CLSI 2019", "CLSI 2018", "CLSI 2017", "CLSI 2016", "CLSI 2015", "CLSI 2014", "CLSI 2013", "CLSI 2012", and "CLSI 2011".
The functions \code{\link[=scale_x_mic]{scale_x_mic()}}, \code{\link[=scale_y_mic]{scale_y_mic()}}, \code{\link[=scale_colour_mic]{scale_colour_mic()}}, and \code{\link[=scale_fill_mic]{scale_fill_mic()}} functions allow to plot the \link[=as.mic]{mic} class (MIC values) on a continuous scale. They allow to rescale the MIC range, and retain the signs in MIC values if desired. Missing intermediate log2 levels will be plotted too.
}
\subsection{The \verb{scale_*_sir()} Functions}{
The functions \code{\link[=scale_x_sir]{scale_x_sir()}}, \code{\link[=scale_colour_sir]{scale_colour_sir()}}, and \code{\link[=scale_fill_sir]{scale_fill_sir()}} functions allow to plot the \link[=as.sir]{sir} class (S/I/R values). They can translate the S/I/R values to any of the 20 supported languages, and set colour-blind friendly colours to the \code{colour} and \code{fill} aesthetics.
}
Simply using \code{"CLSI"} or \code{"EUCAST"} as input will automatically select the latest version of that guideline.
\subsection{Additional \code{ggplot2} Functions}{
This package contains several functions that extend the \code{ggplot2} package, to help in visualising AMR data results. All these functions are internally used by \code{\link[=ggplot_sir]{ggplot_sir()}} too.
This package contains more functions that extend the \code{ggplot2} package, to help in visualising AMR data results. All these functions are internally used by \code{\link[=ggplot_sir]{ggplot_sir()}} too.
\itemize{
\item \code{\link[=facet_sir]{facet_sir()}} creates 2d plots (at default based on S/I/R) using \code{\link[ggplot2:facet_wrap]{ggplot2::facet_wrap()}}.
\item \code{\link[=scale_y_percent]{scale_y_percent()}} transforms the y axis to a 0 to 100\% range using \code{\link[ggplot2:scale_continuous]{ggplot2::scale_y_continuous()}}.
\item \code{\link[=scale_sir_colours]{scale_sir_colours()}} sets colours to the bars (green for S, yellow for I, and red for R). Has multilingual support. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red.
\item \code{\link[=scale_sir_colours]{scale_sir_colours()}} allows to set colours to any aesthetic, even for \code{shape} or \code{linetype}.
\item \code{\link[=theme_sir]{theme_sir()}} is a [ggplot2 theme][\code{\link[ggplot2:theme]{ggplot2::theme()}} with minimal distraction.
\item \code{\link[=labels_sir_count]{labels_sir_count()}} print datalabels on the bars with percentage and number of isolates, using \code{\link[ggplot2:geom_text]{ggplot2::geom_text()}}.
}
The interpretation of "I" will be named "Increased exposure" for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
For interpreting MIC values as well as disk diffusion diameters, supported guidelines to be used as input for the \code{guideline} argument are: "EUCAST 2024", "EUCAST 2023", "EUCAST 2022", "EUCAST 2021", "EUCAST 2020", "EUCAST 2019", "EUCAST 2018", "EUCAST 2017", "EUCAST 2016", "EUCAST 2015", "EUCAST 2014", "EUCAST 2013", "EUCAST 2012", "EUCAST 2011", "CLSI 2024", "CLSI 2023", "CLSI 2022", "CLSI 2021", "CLSI 2020", "CLSI 2019", "CLSI 2018", "CLSI 2017", "CLSI 2016", "CLSI 2015", "CLSI 2014", "CLSI 2013", "CLSI 2012", and "CLSI 2011". Simply using \code{"CLSI"} or \code{"EUCAST"} as input will automatically select the latest version of that guideline.
}
}
\examples{
@ -183,6 +198,9 @@ some_mic_values <- random_mic(size = 100)
some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro")
some_sir_values <- random_sir(50, prob_SIR = c(0.55, 0.05, 0.30))
# Plotting using base R's plot() ---------------------------------------
plot(some_mic_values)
plot(some_disk_values)
plot(some_sir_values)
@ -224,8 +242,10 @@ 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),
@ -243,8 +263,10 @@ if (require("ggplot2")) {
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,
@ -254,7 +276,45 @@ if (require("ggplot2")) {
scale_fill_mic(mic_range = c(0.5, 16))
}
# Auto plotting --------------------------------------------------------
# Plotting using scale_x_sir() -----------------------------------------
if (require("ggplot2")) {
ggplot(data.frame(x = c("I", "R", "S"),
y = c(45,323, 573)),
aes(x, y)) +
geom_col() +
scale_x_sir()
}
# Plotting using scale_y_mic() and scale_colour_sir() ------------------
if (require("ggplot2")) {
plain <- ggplot(data.frame(mic = some_mic_values,
group = some_groups,
sir = as.sir(some_mic_values,
mo = "E. coli",
ab = "cipro")),
aes(x = group, y = mic, colour = sir)) +
theme_minimal() +
geom_boxplot(fill = NA, colour = "grey") +
geom_jitter(width = 0.25)
plain
}
if (require("ggplot2")) {
# and now with our MIC and SIR scale functions:
plain +
scale_y_mic() +
scale_colour_sir()
}
if (require("ggplot2")) {
plain +
scale_y_mic(mic_range = c(0.005, 32), name = "Our MICs!") +
scale_colour_sir(language = "el", eucast_I = FALSE,
name = "In Greek!")
}
# Plotting using ggplot2's autoplot() ----------------------------------
if (require("ggplot2")) {
autoplot(some_mic_values)
}
@ -265,6 +325,7 @@ if (require("ggplot2")) {
autoplot(some_sir_values)
}
# Plotting using scale_y_percent() -------------------------------------
if (require("ggplot2")) {
p <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")),

View File

@ -0,0 +1,87 @@
# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in The Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
if (AMR:::pkg_is_available("ggplot2", also_load = TRUE)) {
pdf(NULL) # prevent Rplots.pdf being created
# scale_*_mic
aesthetics <- c("x", "y", "colour", "fill")
expected_methods <- c("transform", "transform_df", "breaks", "labels", "limits")
for (aest in aesthetics) {
scale_fn_name <- paste0("scale_", aest, "_continuous")
scale_obj <- getExportedValue("ggplot2", scale_fn_name)()
for (method in expected_methods) {
expect_true(is.function(scale_obj[[method]]) || method %in% names(scale_obj),
info = paste0("Method '", method, "' is missing in ggplot2::", scale_fn_name))
}
}
# scale_*_sir
aesthetics <- c("colour", "fill")
expected_methods <- c("transform", "transform_df", "labels", "limits")
for (aest in aesthetics) {
scale_fn_name <- "scale_discrete_manual"
scale_obj <- getExportedValue("ggplot2", scale_fn_name)(aesthetics = aest)
for (method in expected_methods) {
expect_true(is.function(scale_obj[[method]]) || method %in% names(scale_obj),
info = paste0("Method '", method, "' is missing in ggplot2::", scale_fn_name))
}
}
for (method in expected_methods) {
expect_true(is.function(ggplot2::scale_x_discrete()[[method]]) || method %in% names(ggplot2::scale_x_discrete()),
info = paste0("Method '", method, "' is missing in ggplot2::", "scale_x_discrete"))
}
expect_inherits(ggplot(data.frame(count = c(1,2,3, 4),
sir = c("S", "I", "R", "SDD")),
aes(x = sir, y = count, fill = sir)) +
geom_col() +
scale_x_sir(eucast_I = F, language = "el") +
scale_fill_sir(eucast_I = T, language = "nl"),
"gg")
expect_inherits(ggplot(data.frame(mic = as.mic(c(2,4,8, 16)),
sir = as.sir(c("S", "I", "R", "SDD"))),
aes(x = sir, y = mic)) +
geom_point() +
scale_y_mic(),
"gg")
expect_inherits(ggplot(data.frame(mic = as.mic(c(2,4,8, 16)),
sir = as.sir(c("S", "I", "R", "SDD"))),
aes(x = sir, y = mic)) +
geom_col() +
scale_y_mic(),
"gg")
expect_inherits(ggplot(data.frame(mic = as.mic(c(2,4,8, 16)),
sir = as.sir(c("S", "I", "R", "SDD"))),
aes(x = sir, y = mic)) +
geom_col() +
scale_y_mic(mic_range = c(4,16)) +
scale_x_sir(),
"gg")
}