From 2f866985c9cfe54d0127c9e7f6eacfedf2dc5a03 Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Wed, 23 Jul 2025 22:05:20 +0200 Subject: [PATCH] (v3.0.0.9016) fix for plotting --- DESCRIPTION | 4 ++-- NEWS.md | 2 +- R/plotting.R | 31 ++++++++++++++++++------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 97b3b2c3e..dd397f7d7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 3.0.0.9015 -Date: 2025-07-19 +Version: 3.0.0.9016 +Date: 2025-07-23 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/NEWS.md b/NEWS.md index 28726c0d2..481a63302 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 3.0.0.9015 +# AMR 3.0.0.9016 This is primarily a bugfix release, though we added one nice feature too. diff --git a/R/plotting.R b/R/plotting.R index faac2445d..81464e18e 100755 --- a/R/plotting.R +++ b/R/plotting.R @@ -381,7 +381,7 @@ create_scale_sir <- function(aesthetics, colours_SIR, language, eucast_I, ...) { args <- list(...) args[c("value", "labels", "limits")] <- NULL - colours_SIR <- expand_SIR_colours(colours_SIR) + colours_SIR <- expand_SIR_colours(colours_SIR, unname = FALSE) if (identical(aesthetics, "x")) { ggplot_fn <- ggplot2::scale_x_discrete @@ -391,24 +391,19 @@ create_scale_sir <- function(aesthetics, colours_SIR, language, eucast_I, ...) { args, list( aesthetics = aesthetics, - values = c( - S = colours_SIR[1], - SDD = colours_SIR[2], - I = colours_SIR[3], - R = colours_SIR[4], - NI = "grey30" - ) + values = c(colours_SIR, NI = "grey30") ) ) } scale <- do.call(ggplot_fn, args) scale$labels <- function(x) { - stop_ifnot(all(x %in% c(levels(NA_sir_), NA)), + stop_ifnot(all(x %in% c(levels(NA_sir_), "SI", "IR", NA)), "Apply `scale_", aesthetics[1], "_sir()` to a variable of class 'sir', see `?as.sir`.", call = FALSE ) - x <- as.character(as.sir(x)) + x <- as.character(x) + x[!x %in% c("SI", "IR")] <- as.character(as.sir(x[!x %in% c("SI", "IR")])) if (!is.null(language)) { x[x == "S"] <- "(S) Susceptible" x[x == "SDD"] <- "(SDD) Susceptible dose-dependent" @@ -418,6 +413,8 @@ create_scale_sir <- function(aesthetics, colours_SIR, language, eucast_I, ...) { x[x == "I"] <- "(I) Intermediate" } x[x == "R"] <- "(R) Resistant" + x[x == "SI"] <- "(S/I) Susceptible" + x[x == "IR"] <- "(I/R) Non-susceptible" x[x == "NI"] <- "(NI) Non-interpretable" x <- translate_AMR(x, language = language) } @@ -425,7 +422,7 @@ create_scale_sir <- function(aesthetics, colours_SIR, language, eucast_I, ...) { } scale$limits <- function(x, ...) { # force SIR in the right order - as.character(sort(factor(x, levels = levels(NA_sir_)))) + as.character(sort(factor(x, levels = c(levels(NA_sir_), "SI", "IR")))) } scale @@ -1490,7 +1487,7 @@ labels_sir_count <- function(position = NULL, } expand_SIR_colours <- function(colours_SIR, unname = TRUE) { - sir_order <- c("S", "SDD", "SI", "I", "IR", "R") + sir_order <- c("S", "SDD", "I", "R", "SI", "IR") if (is.null(names(colours_SIR))) { if (length(colours_SIR) == 1) { @@ -1500,13 +1497,21 @@ expand_SIR_colours <- function(colours_SIR, unname = TRUE) { # fill in green for SDD as extra colour colours_SIR <- c(colours_SIR[1], colours_SIR[1], colours_SIR[2], colours_SIR[3]) } + if (length(colours_SIR) == 4) { + # add colours for SI (same as S) and IR (same as R) + colours_SIR <- c(colours_SIR[1:4], colours_SIR[1], colours_SIR[4]) + } names(colours_SIR) <- sir_order } else { # named input: match and reorder stop_ifnot( all(names(colours_SIR) %in% sir_order), - "Unknown names in `colours_SIR`. Expected any of: ", vector_or(sir_order, quotes = FALSE, sort = FALSE), "." + "Unknown names in `colours_SIR`. Expected any of: ", vector_or(levels(NA_sir_), quotes = FALSE, sort = FALSE), "." ) + if (length(colours_SIR) == 4) { + # add colours for SI (same as S) and IR (same as R) + colours_SIR <- c(colours_SIR[1:4], SI = unname(colours_SIR[1]), IR = unname(colours_SIR[4])) + } colours_SIR <- colours_SIR[sir_order] }