From a558f4c1210faf24437281cae003576731be528b Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Tue, 24 Sep 2024 22:39:40 +0200 Subject: [PATCH] (v2.1.1.9080) fix `rescale_mic()` for an outside MIC range --- DESCRIPTION | 2 +- NEWS.md | 2 +- R/mic.R | 6 +++--- R/{plot.R => plotting.R} | 20 ++++++++++++++++---- R/sir.R | 2 +- man/plot.Rd | 13 +++++++++---- 6 files changed, 31 insertions(+), 14 deletions(-) rename R/{plot.R => plotting.R} (97%) diff --git a/DESCRIPTION b/DESCRIPTION index ec1703f7..91689245 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: AMR -Version: 2.1.1.9079 +Version: 2.1.1.9080 Date: 2024-09-24 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) diff --git a/NEWS.md b/NEWS.md index cde2a189..265d7702 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 2.1.1.9079 +# AMR 2.1.1.9080 *(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/R/mic.R b/R/mic.R index 466fa5a3..02087269 100644 --- a/R/mic.R +++ b/R/mic.R @@ -304,9 +304,9 @@ rescale_mic <- function(x, mic_range, keep_operators = "edges", as.mic = TRUE) { # create a manual factor with levels only within desired range expanded <- plotrange_as_table(x, - expand = TRUE, - keep_operators = ifelse(keep_operators == "edges", "none", keep_operators), - mic_range = mic_range) + expand = TRUE, + keep_operators = ifelse(keep_operators == "edges", "none", keep_operators), + mic_range = mic_range) if (keep_operators == "edges") { names(expanded)[1] <- paste0("<=", names(expanded)[1]) names(expanded)[length(expanded)] <- paste0(">=", names(expanded)[length(expanded)]) diff --git a/R/plot.R b/R/plotting.R similarity index 97% rename from R/plot.R rename to R/plotting.R index 38b58a1b..1ea16e64 100755 --- a/R/plot.R +++ b/R/plotting.R @@ -73,7 +73,7 @@ #' # Plotting using scale_x_mic() #' \donttest{ #' if (require("ggplot2")) { -#' mic_plot <- ggplot(data.frame(mics = as.mic(c(0.125, "<=4", 4, 8, 32, ">=32")), +#' mic_plot <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), #' counts = c(1, 1, 2, 2, 3, 3)), #' aes(mics, counts)) + #' geom_col() @@ -92,8 +92,13 @@ #' } #' if (require("ggplot2")) { #' mic_plot + -#' scale_x_mic(mic_range = c(1, 128)) + -#' labs(title = "with scale_x_mic() using a manual range") +#' scale_x_mic(mic_range = c(1, 16)) + +#' labs(title = "with scale_x_mic() using a manual 'within' range") +#' } +#' if (require("ggplot2")) { +#' mic_plot + +#' scale_x_mic(mic_range = c(0.032, 256)) + +#' labs(title = "with scale_x_mic() using a manual 'outside' range") #' } #' #' if (require("ggplot2")) { @@ -795,7 +800,14 @@ plotrange_as_table <- function(x, expand, keep_operators = "all", mic_range = NU x <- as.mic(x, keep_operators = keep_operators) if (expand == TRUE) { # expand range for MIC by adding common intermediate factors levels - extra_range <- COMMON_MIC_VALUES[COMMON_MIC_VALUES > min(x, na.rm = TRUE) & COMMON_MIC_VALUES < max(x, na.rm = TRUE)] + if (!is.null(mic_range) && !all(is.na(mic_range))) { + # base on mic_range + `%na_or%` <- function(x, y) if (is.na(x)) y else x + extra_range <- COMMON_MIC_VALUES[COMMON_MIC_VALUES >= (mic_range[1] %na_or% min(x, na.rm = TRUE)) & COMMON_MIC_VALUES <= (mic_range[2] %na_or% max(x, na.rm = TRUE))] + } else { + # base on x + extra_range <- COMMON_MIC_VALUES[COMMON_MIC_VALUES > min(x, na.rm = TRUE) & COMMON_MIC_VALUES < max(x, na.rm = TRUE)] + } # remove the ones that are in 25% range of user values extra_range <- extra_range[!vapply(FUN.VALUE = logical(1), extra_range, function(r) any(abs(r - x) / x < 0.25, na.rm = TRUE))] nms <- extra_range diff --git a/R/sir.R b/R/sir.R index c413f4e1..e8a94406 100755 --- a/R/sir.R +++ b/R/sir.R @@ -1376,7 +1376,7 @@ as_sir_method <- function(method_short, } else { site <- paste0("body site '", site, "'") } - if (nrow(breakpoints_current) == 1 && all(breakpoints_current$uti == TRUE) && any(uti_current %in% c(FALSE, NA)) && message_not_thrown_before("as.sir", "uti", ab_current)) { + if (nrow(breakpoints_current) == 1 && all(breakpoints_current$uti == TRUE) && is.na(uti_current) && message_not_thrown_before("as.sir", "uti", ab_current)) { # only UTI breakpoints available notes_current <- c(notes_current, paste0("Breakpoints for ", font_bold(ab_formatted), " in ", mo_formatted, " are only available for (uncomplicated) urinary tract infections (UTI); assuming `uti = TRUE`.")) } else if (nrow(breakpoints_current) > 1 && length(unique(breakpoints_current$site)) > 1 && any(is.na(uti_current)) && all(c(TRUE, FALSE) %in% breakpoints_current$uti, na.rm = TRUE) && message_not_thrown_before("as.sir", "siteUTI", mo_current, ab_current)) { diff --git a/man/plot.Rd b/man/plot.Rd index 08d5f43a..29d41628 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plot.R +% Please edit documentation in R/plotting.R \name{plot} \alias{plot} \alias{scale_x_mic} @@ -180,7 +180,7 @@ plot(some_disk_values, mo = "Escherichia coli", ab = "cipro", language = "nl") # Plotting using scale_x_mic() \donttest{ if (require("ggplot2")) { - mic_plot <- ggplot(data.frame(mics = as.mic(c(0.125, "<=4", 4, 8, 32, ">=32")), + mic_plot <- ggplot(data.frame(mics = as.mic(c(0.25, "<=4", 4, 8, 32, ">=32")), counts = c(1, 1, 2, 2, 3, 3)), aes(mics, counts)) + geom_col() @@ -199,8 +199,13 @@ if (require("ggplot2")) { } if (require("ggplot2")) { mic_plot + - scale_x_mic(mic_range = c(1, 128)) + - labs(title = "with scale_x_mic() using a manual range") + scale_x_mic(mic_range = c(1, 16)) + + labs(title = "with scale_x_mic() using a manual 'within' range") +} +if (require("ggplot2")) { + mic_plot + + scale_x_mic(mic_range = c(0.032, 256)) + + labs(title = "with scale_x_mic() using a manual 'outside' range") } if (require("ggplot2")) {