(v3.1.0.9017) Add labels_taxonomy() for taxonomic names in italics in plot labels, add type = "plotmath" to italicise_taxonomy(), and only italicise species names after a genus

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2026-11-09 20:45:07 +01:00
1 parent db7377f517
commit cfb55a4c3e
14 files changed
+272 -79

No files matched your search

+26 -1
View File
@@ -33,7 +33,7 @@
#' Functions to plot classes `sir`, `mic` and `disk`, with support for base \R and `ggplot2`.
#'
#' Especially the `scale_*_mic()` functions are relevant wrappers to plot MIC values for `ggplot2`. They allow custom MIC ranges and to plot intermediate log2 levels for missing MIC values.
#' @param x,object Values created with [as.mic()], [as.disk()] or [as.sir()] (or their `random_*` variants, such as [random_mic()]).
#' @param x,object Values created with [as.mic()], [as.disk()] or [as.sir()] (or their `random_*` variants, such as [random_mic()]). For [labels_taxonomy()], any values to use as labels, such as the breaks of an axis.
#' @param mo Any (vector of) text that can be coerced to a valid microorganism code with [as.mo()].
#' @param ab Any (vector of) text that can be coerced to a valid antimicrobial drug code with [as.ab()].
#' @param guideline Interpretation guideline to use - the default is the latest included EUCAST guideline, see *Details*.
@@ -76,6 +76,10 @@
#' * [theme_sir()] is a [ggplot2 theme][ggplot2::theme()] with minimal distraction.
#' * [labels_sir_count()] prints data labels on the bars with percentage and number of isolates, using [ggplot2::geom_text()].
#'
#' ### Taxonomic Names in Labels
#'
#' The function [labels_taxonomy()] prints taxonomic names in axis and legend labels in italics, as is the convention in microbiology, e.g., *E. coli* and *Klebsiella pneumoniae*. It can directly be used as labeller in `ggplot2`, e.g., `scale_x_discrete(labels = labels_taxonomy)`, and in base \R, e.g., `axis(1, at = 1:3, labels = labels_taxonomy(names))`. Other words, such as "Other" or "CoNS", stay upright. It uses [italicise_taxonomy()] with `type = "plotmath"`, which also works for titles. It returns an [expression] vector of the same length as `x`; other arguments in `...` are ignored.
#'
#' The interpretation of "I" will be named "Susceptible, incr. exp." (susceptible, 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, the default guideline is `r AMR::clinical_breakpoints$guideline[1]`, unless the package option [`AMR_guideline`][AMR-options] is set. See [as.sir()] for more information.
@@ -225,6 +229,18 @@
#' plot(some_disk_values, mo = "Escherichia coli", ab = "cipro", language = "nl")
#'
#' plot(some_sir_values)
#'
#' # Taxonomic names in italics using labels_taxonomy() ------------------
#' bacteria <- data.frame(
#' name = c("E. coli", "Klebsiella pneumoniae", "CoNS", "Other"),
#' n = c(25, 18, 9, 6)
#' )
#' barplot(bacteria$n, names.arg = labels_taxonomy(bacteria$name))
#' if (require("ggplot2")) {
#' ggplot(bacteria, aes(name, n)) +
#' geom_col() +
#' scale_x_discrete(labels = labels_taxonomy)
#' }
NULL
#' @rawNamespace if(getRversion() >= "3.0.0") S3method(ggplot2::scale_type, mic)
@@ -1412,6 +1428,15 @@ theme_sir <- function() {
)
}
#' @rdname plot
#' @export
labels_taxonomy <- function(x, ...) {
if (length(x) == 0) {
return(expression())
}
italicise_taxonomy(as.character(x), type = "plotmath")
}
#' @rdname ggplot_sir
#' @export
labels_sir_count <- function(position = NULL,