diff --git a/DESCRIPTION b/DESCRIPTION index 05f2dc3a..ec0a79c4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.5.0.9026 -Date: 2021-02-25 +Version: 1.5.0.9027 +Date: 2021-02-26 Title: Antimicrobial Resistance Data Analysis Authors@R: c( person(role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index 7aaf7e1f..bcc131ca 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,5 @@ -# AMR 1.5.0.9026 -## Last updated: 25 February 2021 +# AMR 1.5.0.9027 +## Last updated: 26 February 2021 ### New * Support for EUCAST Clinical Breakpoints v11.0 (2021), effective in the `eucast_rules()` function and in `as.rsi()` to interpret MIC and disk diffusion values. This is now the default guideline in this package. diff --git a/R/ggplot_rsi.R b/R/ggplot_rsi.R index 5de41893..b995d855 100755 --- a/R/ggplot_rsi.R +++ b/R/ggplot_rsi.R @@ -36,8 +36,8 @@ #' @param facet variable to split plots by, either `"interpretation"` (default) or `"antibiotic"` or a grouping variable #' @inheritParams proportion #' @param nrow (when using `facet`) number of rows -#' @param colours a named vector with colours for the bars. The names must be one or more of: S, SI, I, IR, R or be `FALSE` for standard [ggplot2][ggplot2::ggplot()] colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red. -#' @param aesthetics aesthetics to apply the colours to, defaults to "fill" but can also be "colour" or "both" +#' @param colours a named vactor with colour to be used for filling. The default colours are colour-blind friendly. +#' @param aesthetics aesthetics to apply the colours to, defaults to "fill" but can also be (a combination of) "alpha", "colour", "fill", "linetype", "shape" or "size" #' @param datalabels show datalabels using [labels_rsi_count()] #' @param datalabels.size size of the datalabels #' @param datalabels.colour colour of the datalabels @@ -46,7 +46,7 @@ #' @param caption text to show as caption of the plot #' @param x.title text to show as x axis description #' @param y.title text to show as y axis description -#' @param ... other arguments passed on to [geom_rsi()] +#' @param ... other arguments passed on to [geom_rsi()] or, in case of [scale_rsi_colours()], named values to set colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red. See *Examples*. #' @details At default, the names of antibiotics will be shown on the plots using [ab_name()]. This can be set with the `translate_ab` argument. See [count_df()]. #' #' ## The Functions @@ -56,7 +56,7 @@ #' #' [scale_y_percent()] transforms the y axis to a 0 to 100% range using [ggplot2::scale_y_continuous()]. #' -#' [scale_rsi_colours()] sets colours to the bars: pastel blue for S, pastel turquoise for I and pastel red for R, using [ggplot2::scale_fill_manual()]. +#' [scale_rsi_colours()] sets colours to the bars (green for S, yellow for I, and red for R). with 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. #' #' [theme_rsi()] is a [ggplot2 theme][[ggplot2::theme()] with minimal distraction. #' @@ -219,11 +219,6 @@ ggplot_rsi <- function(data, theme_rsi() if (fill == "interpretation") { - # set RSI colours - if (isFALSE(colours) & missing(datalabels.colour)) { - # set datalabel colour to middle grey - datalabels.colour <- "grey50" - } p <- p + scale_rsi_colours(colours = colours) } @@ -362,28 +357,50 @@ scale_y_percent <- function(breaks = seq(0, 1, 0.1), limits = NULL) { #' @rdname ggplot_rsi #' @export -scale_rsi_colours <- function(colours = c(S = "#3CAEA3", - SI = "#3CAEA3", - I = "#F6D55C", - IR = "#ED553B", - R = "#ED553B"), +scale_rsi_colours <- function(..., aesthetics = "fill") { stop_ifnot_installed("ggplot2") - meet_criteria(colours, allow_class = c("character", "logical")) - meet_criteria(aesthetics, allow_class = c("character"), has_length = c(1, 2), is_in = c("colour", "color", "fill", "both")) + meet_criteria(aesthetics, allow_class = c("character"), has_length = c(1, 2), is_in = c("alpha", "colour", "color", "fill", "linetype", "shape", "size")) - if (!identical(colours, FALSE)) { - if ("both" %in% aesthetics) { - aesthetics <- c("colour", "fill") - } + # behaviour until AMR pkg v1.5.0 and also when coming from ggplot_rsi() + if ("colours" %in% names(list(...))) { original_cols <- c(S = "#3CAEA3", SI = "#3CAEA3", I = "#F6D55C", IR = "#ED553B", R = "#ED553B") - colours <- replace(original_cols, names(colours), colours) - ggplot2::scale_fill_manual(values = colours, aesthetics = aesthetics) + colours <- replace(original_cols, names(list(...)$colours), list(...)$colours) + return(ggplot2::scale_fill_manual(values = colours)) } + if (identical(unlist(list(...)), FALSE)) { + return(invisible()) + } + + names_susceptible <- c("S", "SI", "IS", "S+I", "I+S", "susceptible", + unique(translations_file[which(translations_file$pattern == "susceptible"), + "replacement", drop = TRUE])) + names_incr_exposure <- c("I", "intermediate", "increased exposure", "incr. exposure", + unique(translations_file[which(translations_file$pattern == "intermediate"), + "replacement", drop = TRUE])) + names_resistant <- c("R", "IR", "RI", "R+I", "I+R", "resistant", + unique(translations_file[which(translations_file$pattern == "resistant"), + "replacement", drop = TRUE])) + + susceptible <- rep("#3CAEA3", length(names_susceptible)) + names(susceptible) <- names_susceptible + incr_exposure <- rep("#F6D55C", length(names_incr_exposure)) + names(incr_exposure) <- names_incr_exposure + resistant <- rep("#ED553B", length(names_resistant)) + names(resistant) <- names_resistant + + original_cols = c(susceptible, incr_exposure, resistant) + dots <- c(...) + # replace S, I, R as colours: scale_rsi_colours(mydatavalue = "S") + dots[dots == "S"] <- "#3CAEA3" + dots[dots == "I"] <- "#F6D55C" + dots[dots == "R"] <- "#ED553B" + colours <- replace(original_cols, names(dots), dots) + ggplot2::scale_discrete_manual(aesthetics = aesthetics, values = colours) } #' @rdname ggplot_rsi diff --git a/R/plot.R b/R/plot.R index 39b4fe4d..ccc12ef6 100644 --- a/R/plot.R +++ b/R/plot.R @@ -50,9 +50,11 @@ #' @examples #' some_mic_values <- random_mic(size = 100) #' some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro") +#' some_rsi_values <- random_rsi(50, prob_RSI = c(0.30, 0.55, 0.05)) #' #' plot(some_mic_values) #' plot(some_disk_values) +#' plot(some_rsi_values) #' #' # when providing the microorganism and antibiotic, colours will show interpretations: #' plot(some_mic_values, mo = "S. aureus", ab = "ampicillin") @@ -61,6 +63,7 @@ #' if (require("ggplot2")) { #' ggplot(some_mic_values) #' ggplot(some_disk_values, mo = "Escherichia coli", ab = "cipro") +#' ggplot(some_rsi_values) #' } NULL @@ -229,7 +232,7 @@ ggplot.mic <- function(data, name = NULL) } else { p <- p + - ggplot2::geom_col(aes(x = mic, y = count)) + ggplot2::geom_col(ggplot2::aes(x = mic, y = count)) } p + @@ -242,7 +245,7 @@ ggplot.mic <- function(data, #' @importFrom graphics barplot axis mtext legend #' @rdname plot plot.disk <- function(x, - main = paste("Disk zones values of", deparse(substitute(x))), + main = paste("Disk zones of", deparse(substitute(x))), ylab = "Frequency", xlab = "Disk diffusion diameter (mm)", mo = NULL, @@ -315,7 +318,7 @@ plot.disk <- function(x, #' @export #' @noRd barplot.disk <- function(height, - main = paste("Disk zones values of", deparse(substitute(height))), + main = paste("Disk zones of", deparse(substitute(height))), ylab = "Frequency", xlab = "Disk diffusion diameter (mm)", mo = NULL, @@ -350,7 +353,7 @@ barplot.disk <- function(height, # will be exported using s3_register() in R/zzz.R ggplot.disk <- function(data, mapping = NULL, - title = paste("Disk zones values of", deparse(substitute(data))), + title = paste("Disk zones of", deparse(substitute(data))), ylab = "Frequency", xlab = "Disk diffusion diameter (mm)", mo = NULL, @@ -395,7 +398,7 @@ ggplot.disk <- function(data, if (any(colours_RSI %in% cols_sub$cols)) { p <- p + - ggplot2::geom_col(aes(x = disk, y = count, fill = cols)) + + ggplot2::geom_col(ggplot2::aes(x = disk, y = count, fill = cols)) + ggplot2::scale_fill_manual(values = c("Resistant" = colours_RSI[1], "Susceptible" = colours_RSI[2], "Incr. exposure" = colours_RSI[3], @@ -403,7 +406,7 @@ ggplot.disk <- function(data, name = NULL) } else { p <- p + - ggplot2::geom_col(aes(x = disk, y = count)) + ggplot2::geom_col(ggplot2::aes(x = disk, y = count)) } p + @@ -514,7 +517,7 @@ plot.rsi <- function(x, stringsAsFactors = FALSE) } - data$x <- factor(data$x, levels = c("R", "S", "I"), ordered = TRUE) + data$x <- factor(data$x, levels = c("S", "I", "R"), ordered = TRUE) ymax <- pm_if_else(max(data$s) > 95, 105, 100) @@ -558,7 +561,7 @@ barplot.rsi <- function(height, main <- gsub(" +", " ", paste0(main, collapse = " ")) x <- table(height) - x <- x[c(3, 1, 2)] + x <- x[c(1, 2, 3)] barplot(x, col = colours_RSI, xlab = xlab, @@ -567,3 +570,39 @@ barplot.rsi <- function(height, axes = FALSE) axis(2, seq(0, max(x))) } + +#' @method ggplot rsi +#' @rdname plot +# will be exported using s3_register() in R/zzz.R +ggplot.rsi <- function(data, + mapping = NULL, + title = paste("Resistance Overview of", deparse(substitute(data))), + xlab = "Antimicrobial Interpretation", + ylab = "Frequency", + colours_RSI = c("#ED553B", "#3CAEA3", "#F6D55C"), + ...) { + stop_ifnot_installed("ggplot2") + meet_criteria(title, allow_class = "character") + meet_criteria(ylab, allow_class = "character", has_length = 1) + meet_criteria(xlab, allow_class = "character", has_length = 1) + meet_criteria(colours_RSI, allow_class = "character", has_length = c(1, 3)) + if (length(colours_RSI) == 1) { + colours_RSI <- rep(colours_RSI, 3) + } + + df <- as.data.frame(table(data), stringsAsFactors = TRUE) + colnames(df) <- c("rsi", "count") + if (!is.null(mapping)) { + p <- ggplot2::ggplot(df, mapping = mapping) + } else { + p <- ggplot2::ggplot(df) + } + + p + + ggplot2::geom_col(ggplot2::aes(x = rsi, y = count, fill = rsi)) + + ggplot2::scale_fill_manual(values = c("R" = colours_RSI[1], + "S" = colours_RSI[2], + "I" = colours_RSI[3])) + + ggplot2::labs(title = title, x = xlab, y = ylab) + + ggplot2::theme(legend.position = "none") +} diff --git a/R/zzz.R b/R/zzz.R index 4b02815b..0a6d3de9 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -50,6 +50,7 @@ pkg_env$mo_failed <- character(0) s3_register("skimr::get_skimmers", "rsi") s3_register("skimr::get_skimmers", "mic") s3_register("skimr::get_skimmers", "disk") + s3_register("ggplot2::ggplot", "rsi") s3_register("ggplot2::ggplot", "mic") s3_register("ggplot2::ggplot", "disk") diff --git a/data-raw/AMR_latest.tar.gz b/data-raw/AMR_latest.tar.gz index 4dd2b077..eca1b5a3 100644 Binary files a/data-raw/AMR_latest.tar.gz and b/data-raw/AMR_latest.tar.gz differ diff --git a/docs/404.html b/docs/404.html index e711daef..4fd98338 100644 --- a/docs/404.html +++ b/docs/404.html @@ -81,7 +81,7 @@
diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index f67dc4fb..1a9ba51a 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -81,7 +81,7 @@ diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index 36c71043..6b438568 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -39,7 +39,7 @@ @@ -193,7 +193,7 @@vignettes/AMR.Rmd
AMR.Rmd
Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 25 February 2021.
+Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 26 February 2021.
So only 28.3% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
So only 28.4% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
data_1st <- data %>%
filter(first == TRUE)
We made a slight twist to the CLSI algorithm, to take into account the antimicrobial susceptibility profile. Have a look at all E. coli isolates of patient B7, sorted on date:
+We made a slight twist to the CLSI algorithm, to take into account the antimicrobial susceptibility profile. Have a look at all E. coli isolates of patient Y5, sorted on date:
isolate | @@ -523,10 +523,10 @@ Longest: 1|||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2010-03-16 | -B7 | +2010-01-04 | +Y5 | B_ESCHR_COLI | -R | +S | S | S | S | @@ -534,8 +534,8 @@ Longest: 1|||||
2 | -2010-05-03 | -B7 | +2010-01-13 | +Y5 | B_ESCHR_COLI | S | S | @@ -545,19 +545,19 @@ Longest: 1||||||||
3 | -2010-05-16 | -B7 | +2010-04-23 | +Y5 | B_ESCHR_COLI | +S | +S | R | S | -S | -S | FALSE | |||
4 | -2010-07-25 | -B7 | +2010-06-13 | +Y5 | B_ESCHR_COLI | S | S | @@ -567,19 +567,19 @@ Longest: 1||||||||
5 | -2010-07-26 | -B7 | +2010-09-02 | +Y5 | B_ESCHR_COLI | -R | -R | S | -R | +S | +S | +S | FALSE | ||
6 | -2010-10-14 | -B7 | +2010-11-12 | +Y5 | B_ESCHR_COLI | S | S | @@ -589,10 +589,10 @@ Longest: 1||||||||
7 | -2010-12-24 | -B7 | +2010-11-14 | +Y5 | B_ESCHR_COLI | -R | +S | S | S | S | @@ -600,33 +600,33 @@ Longest: 1|||||
8 | -2011-02-27 | -B7 | +2010-12-31 | +Y5 | B_ESCHR_COLI | -S | -S | +R | +R | S | S | FALSE | |||
9 | -2011-09-02 | -B7 | +2011-03-02 | +Y5 | B_ESCHR_COLI | -S | -S | -S | R | +R | +S | +S | TRUE | ||
10 | -2011-11-08 | -B7 | +2011-06-09 | +Y5 | B_ESCHR_COLI | -I | -I | +R | +R | S | S | FALSE | @@ -657,10 +657,10 @@ Longest: 1|||
1 | -2010-03-16 | -B7 | +2010-01-04 | +Y5 | B_ESCHR_COLI | -R | +S | S | S | S | @@ -669,8 +669,8 @@ Longest: 1|||||
2 | -2010-05-03 | -B7 | +2010-01-13 | +Y5 | B_ESCHR_COLI | S | S | @@ -681,20 +681,20 @@ Longest: 1||||||||
3 | -2010-05-16 | -B7 | +2010-04-23 | +Y5 | B_ESCHR_COLI | +S | +S | R | S | -S | -S | FALSE | -TRUE | +FALSE | |
4 | -2010-07-25 | -B7 | +2010-06-13 | +Y5 | B_ESCHR_COLI | S | S | @@ -705,47 +705,47 @@ Longest: 1||||||||
5 | -2010-07-26 | -B7 | +2010-09-02 | +Y5 | B_ESCHR_COLI | -R | -R | S | -R | +S | +S | +S | +FALSE | FALSE | -TRUE |
6 | -2010-10-14 | -B7 | +2010-11-12 | +Y5 | B_ESCHR_COLI | S | S | S | S | FALSE | -TRUE | +FALSE | |||
7 | -2010-12-24 | -B7 | +2010-11-14 | +Y5 | B_ESCHR_COLI | -R | +S | S | S | S | FALSE | -TRUE | +FALSE | ||
8 | -2011-02-27 | -B7 | +2010-12-31 | +Y5 | B_ESCHR_COLI | -S | -S | +R | +R | S | S | FALSE | @@ -753,36 +753,36 @@ Longest: 1|||
9 | -2011-09-02 | -B7 | +2011-03-02 | +Y5 | B_ESCHR_COLI | -S | -S | -S | R | +R | +S | +S | TRUE | TRUE | |
10 | -2011-11-08 | -B7 | +2011-06-09 | +Y5 | B_ESCHR_COLI | -I | -I | +R | +R | S | S | FALSE | -TRUE | +FALSE |
Instead of 2, now 10 isolates are flagged. In total, 79.1% of all isolates are marked ‘first weighted’ - 50.8% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
+Instead of 2, now 5 isolates are flagged. In total, 78.5% of all isolates are marked ‘first weighted’ - 50.1% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
As with filter_first_isolate()
, there’s a shortcut for this new algorithm too:
data_1st <- data %>%
filter_first_weighted_isolate()
So we end up with 15,821 isolates for analysis.
+So we end up with 15,695 isolates for analysis.
We can remove unneeded columns:
data_1st <- data_1st %>%
@@ -792,7 +792,6 @@ Longest: 1
head(data_1st)
date | patient_id | hospital | @@ -825,13 +823,12 @@ Longest: 1|||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2014-07-28 | -B5 | +2012-05-31 | +J3 | Hospital A | B_ESCHR_COLI | -S | -S | +R | +I | S | S | M | @@ -841,14 +838,13 @@ Longest: 1TRUE | |||
2 | -2017-09-25 | -X1 | +2012-11-27 | +R2 | Hospital B | B_KLBSL_PNMN | R | -I | S | +R | S | F | Gram-negative | @@ -857,67 +853,63 @@ Longest: 1TRUE | |||
3 | -2017-04-18 | -V9 | -Hospital D | -B_KLBSL_PNMN | -R | -S | -S | -S | -F | -Gram-negative | -Klebsiella | -pneumoniae | -TRUE | -||||
5 | -2016-01-08 | -V6 | -Hospital C | -B_KLBSL_PNMN | -R | -S | -S | -S | -F | -Gram-negative | -Klebsiella | -pneumoniae | -TRUE | -||||
6 | -2015-03-14 | -Y2 | +2017-07-10 | +B1 | Hospital B | -B_STPHY_AURS | -S | -S | -S | -S | -F | -Gram-positive | -Staphylococcus | -aureus | -TRUE | -||
7 | -2014-12-23 | -G8 | -Hospital A | -B_STPHY_AURS | -S | +B_STRPT_PNMN | S | S | S | +R | M | Gram-positive | -Staphylococcus | -aureus | +Streptococcus | +pneumoniae | +TRUE | +
2013-11-18 | +B1 | +Hospital B | +B_KLBSL_PNMN | +R | +R | +R | +S | +M | +Gram-negative | +Klebsiella | +pneumoniae | +TRUE | +|||||
2016-07-29 | +O3 | +Hospital A | +B_STRPT_PNMN | +R | +R | +S | +R | +F | +Gram-positive | +Streptococcus | +pneumoniae | +TRUE | +|||||
2016-09-06 | +U3 | +Hospital B | +B_STRPT_PNMN | +S | +S | +S | +R | +F | +Gram-positive | +Streptococcus | +pneumoniae | TRUE | |||||
1 | Escherichia coli | -7,994 | -50.53% | -7,994 | -50.53% | +7,829 | +49.88% | +7,829 | +49.88% | ||||||||
2 | Staphylococcus aureus | -3,923 | -24.80% | -11,917 | +3,992 | +25.43% | +11,821 | 75.32% | |||||||||
3 | Streptococcus pneumoniae | -2,321 | -14.67% | -14,238 | -89.99% | +2,287 | +14.57% | +14,108 | +89.89% | ||||||||
4 | Klebsiella pneumoniae | -1,583 | -10.01% | -15,821 | +1,587 | +10.11% | +15,695 | 100.00% | |||||||||
E. coli | AMX | -3815 | -239 | -3940 | -7994 | +3743 | +231 | +3855 | +7829 | ||||||||
E. coli | AMC | -6289 | -304 | -1401 | -7994 | +6200 | +283 | +1346 | +7829 | ||||||||
E. coli | CIP | -6086 | +5941 | 0 | -1908 | -7994 | +1888 | +7829 | |||||||||
E. coli | GEN | -7158 | +7047 | 0 | -836 | -7994 | +782 | +7829 | |||||||||
K. pneumoniae | AMX | 0 | 0 | -1583 | -1583 | +1587 | +1587 | ||||||||||
K. pneumoniae | AMC | -1224 | -57 | -302 | -1583 | +1242 | +65 | +280 | +1587 |
As per the EUCAST guideline of 2019, we calculate resistance as the proportion of R (proportion_R()
, equal to resistance()
) and susceptibility as the proportion of S and I (proportion_SI()
, equal to susceptibility()
). These functions can be used on their own:
data_1st %>% resistance(AMX)
-# [1] 0.5370078
Or can be used in conjuction with group_by()
and summarise()
, both from the dplyr
package:
data_1st %>%
@@ -1133,19 +1125,19 @@ Longest: 24
Hospital A
-0.5410321
+0.5453191
Hospital B
-0.5381272
+0.5342294
Hospital C
-0.5306981
+0.5395015
Hospital D
-0.5338134
+0.5495407
@@ -1165,23 +1157,23 @@ Longest: 24
Hospital A
-0.5410321
-4728
+0.5453191
+4700
Hospital B
-0.5381272
-5521
+0.5342294
+5580
Hospital C
-0.5306981
-2378
+0.5395015
+2367
Hospital D
-0.5338134
-3194
+0.5495407
+3048
@@ -1203,27 +1195,27 @@ Longest: 24
Escherichia
-0.8247436
-0.8954216
-0.9836127
+0.8280751
+0.9001150
+0.9840337
Klebsiella
-0.8092230
-0.9001895
-0.9823121
+0.8235665
+0.8890989
+0.9823566
Staphylococcus
-0.8179964
-0.9166454
-0.9836860
+0.8274048
+0.9251002
+0.9874749
Streptococcus
-0.5489013
+0.5347617
0.0000000
-0.5489013
+0.5347617
@@ -1308,62 +1300,59 @@ Longest: 24
mic_values <- random_mic(size = 100)
mic_values
# Class <mic>
-# [1] 128 64 0.5 8 64 0.125 16 64 8 2
-# [11] 0.0625 8 1 4 0.25 0.5 0.5 32 1 1
-# [21] 0.25 8 8 0.0625 16 16 2 64 0.125 8
-# [31] 1 0.125 4 0.125 32 16 0.25 1 0.5 128
-# [41] 32 128 64 1 0.125 32 0.125 0.125 8 0.125
-# [51] 64 8 8 0.0625 0.25 0.5 2 2 128 0.25
-# [61] 4 1 1 0.25 64 0.5 32 0.25 16 2
-# [71] 16 8 32 4 0.25 16 128 32 128 4
-# [81] 64 1 0.25 0.5 1 1 4 8 16 16
-# [91] 32 0.5 1 0.125 2 128 0.5 1 4 64
# base R:
plot(mic_values)
# ggplot2:
ggplot(mic_values)
But we could also be more specific, by generating MICs that are likely to be found in E. coli for ciprofloxacin:
-# this will generate MICs that are likely to be found in E. coli for ciprofloxacin:
-mic_values_specific <- random_mic(size = 100, mo = "E. coli", ab = "cipro")
mic_values <- random_mic(size = 100, mo = "E. coli", ab = "cipro")
For the plot()
and ggplot()
function, we can define the microorganism and an antimicrobial agent the same way. This will add the interpretation of those values according to a chosen guidelines (defaults to the latest EUCAST guideline).
Default colours are colour-blind friendly, while maintaining the convention that e.g. ‘susceptible’ should be green and ‘resistant’ should be red:
# base R:
-plot(mic_values_specific, mo = "E. coli", ab = "cipro")
# ggplot2:
-ggplot(mic_values_specific, mo = "E. coli", ab = "cipro")
For disk diffusion values, there is not much of a difference in plotting:
-# this will generate disks that are likely to be found in E. coli for ciprofloxacin:
-disk_values_specific <- random_disk(size = 100, mo = "E. coli", ab = "cipro")
+disk_values <- random_disk(size = 100, mo = "E. coli", ab = "cipro")
# NOTE: Translation to one microorganism was guessed with uncertainty. Use
# `mo_uncertainties()` to review it.
-disk_values_specific
+disk_values
# Class <disk>
-# [1] 20 29 20 26 27 22 18 21 29 23 18 19 29 26 27 24 20 20 23 20 18 25 31 31 29
-# [26] 28 27 31 18 21 18 26 30 20 21 27 24 27 19 24 17 26 24 31 30 28 19 30 19 21
-# [51] 17 19 22 19 23 23 19 20 28 31 31 30 20 17 21 31 29 29 27 21 29 27 26 18 21
-# [76] 29 21 26 29 22 24 18 22 29 30 17 19 23 26 31 17 18 18 28 30 25 22 21 27 17
# base R:
-plot(disk_values_specific, mo = "E. coli", ab = "cipro")
And when using the ggplot2
package, but now choosing the latest implemented CLSI guideline (notice that the EUCAST-specific term “Incr. exposure” has changed to “Intermediate”):
-# and ggplot2, but now choosing an old CLSI guideline:
-ggplot(disk_values_specific,
+ggplot(disk_values,
mo = "E. coli",
ab = "cipro",
guideline = "CLSI")
In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to 200 times slower to determine.
To improve performance, we implemented two important algorithms to save unnecessary calculations: repetitive results and already precalculated results.
@@ -260,8 +260,8 @@ # what do these values look like? They are of class <mo>: head(x) # Class <mo> -# [1] B_ESCHR_COLI B_PROTS_MRBL B_PROTS_MRBL B_PROTS_MRBL B_STPHY_CONS -# [6] B_ENTRC +# [1] B_STRPT_PYGN B_STPHY_HMNS B_STPHY_CONS B_STRPT_SLVR B_ENTRBC_CLOC +# [6] B_STPHY_CONS # as the example_isolates data set has 2,000 rows, we should have 2 million items length(x) @@ -277,8 +277,8 @@ print(run_it, unit = "ms", signif = 3) # Unit: milliseconds # expr min lq mean median uq max neval -# mo_name(x) 160 189 224 201 228 356 10 -So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.201 seconds. That is 101 nanoseconds on average. You only lose time on your unique input values.
+# mo_name(x) 161 194 224 204 229 368 10 +So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.204 seconds. That is 102 nanoseconds on average. You only lose time on your unique input values.
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0018 seconds - it doesn’t even start calculating if the result would be the same as the expected resulting value. That goes for all helper functions:
run_it <- microbenchmark(A = mo_species("aureus"),
@@ -309,14 +309,14 @@
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
-# A 1.25 1.28 1.39 1.39 1.50 1.56 10
-# B 1.17 1.19 1.41 1.43 1.48 1.92 10
-# C 1.20 1.33 1.43 1.46 1.50 1.61 10
-# D 1.43 1.45 1.50 1.49 1.53 1.61 10
-# E 1.26 1.40 1.45 1.43 1.49 1.81 10
-# F 1.15 1.17 1.32 1.26 1.44 1.69 10
-# G 1.19 1.25 1.37 1.35 1.47 1.58 10
-# H 1.20 1.25 1.46 1.31 1.53 2.33 10
Of course, when running mo_phylum("Firmicutes")
the function has zero knowledge about the actual microorganism, namely S. aureus. But since the result would be "Firmicutes"
anyway, there is no point in calculating the result. And because this package contains all phyla of all known bacteria, it can just return the initial value immediately.
Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.
diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png index 71a7c0cf..766c4fa4 100644 Binary files a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png differ diff --git a/docs/articles/index.html b/docs/articles/index.html index af4e1b72..bced51df 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -81,7 +81,7 @@ diff --git a/docs/authors.html b/docs/authors.html index 3e35632f..494c82ae 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -81,7 +81,7 @@ diff --git a/docs/extra.css b/docs/extra.css index a0e2a435..093e035c 100644 --- a/docs/extra.css +++ b/docs/extra.css @@ -207,10 +207,11 @@ div[id^=last-updated] h2 { .table thead th { text-align: inherit; } -.table a:not(.btn) { +/* all tables, including argument lists */ +table a:not(.btn) { text-decoration: inherit; } -.table a:not(.btn):hover { +table a:not(.btn):hover { text-decoration: underline; } diff --git a/docs/index.html b/docs/index.html index 43bfb2c4..cc41dfe0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,7 +43,7 @@ diff --git a/docs/news/index.html b/docs/news/index.html index eaa4cff7..72fb87a8 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -81,7 +81,7 @@ @@ -236,13 +236,13 @@ Source:NEWS.md
- a named vector with colours for the bars. The names must be one or more of: S, SI, I, IR, R or be FALSE
for standard ggplot2 colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red.
a named vactor with colour to be used for filling. The default colours are colour-blind friendly.
other arguments passed on to geom_rsi()
other arguments passed on to geom_rsi()
or, in case of scale_rsi_colours()
, named values to set colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red. See Examples.
aesthetics to apply the colours to, defaults to "fill" but can also be "colour" or "both"
aesthetics to apply the colours to, defaults to "fill" but can also be (a combination of) "alpha", "colour", "fill", "linetype", "shape" or "size"
geom_rsi()
will take any variable from the data that has an rsi
class (created with as.rsi()
) using rsi_df()
and will plot bars with the percentage R, I and S. The default behaviour is to have the bars stacked and to have the different antibiotics on the x axis.
facet_rsi()
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_rsi_colours()
sets colours to the bars: pastel blue for S, pastel turquoise for I and pastel red for R, using ggplot2::scale_fill_manual()
.
scale_rsi_colours()
sets colours to the bars (green for S, yellow for I, and red for R). with 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.
theme_rsi()
is a [ggplot2 theme][ggplot2::theme()
with minimal distraction.
labels_rsi_count()
print datalabels on the bars with percentage and amount of isolates using ggplot2::geom_text()
.
ggplot_rsi()
is a wrapper around all above functions that uses data as first input. This makes it possible to use this function after a pipe (%>%
). See Examples.
plot(<mic>)
ggplot(<mic>)
plot(<disk>)
ggplot(<disk>)
plot(<rsi>)
plot(<mic>)
ggplot(<mic>)
plot(<disk>)
ggplot(<disk>)
plot(<rsi>)
ggplot(<rsi>)
Plotting for Classes rsi
, mic
and disk
some_mic_values <- random_mic(size = 100) some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro") +some_rsi_values <- random_rsi(50, prob_RSI = c(0.30, 0.55, 0.05)) plot(some_mic_values) plot(some_disk_values) +plot(some_rsi_values) # when providing the microorganism and antibiotic, colours will show interpretations: plot(some_mic_values, mo = "S. aureus", ab = "ampicillin") @@ -389,6 +402,7 @@ The lifecycle of this function is stableif (require("ggplot2")) { ggplot(some_mic_values) ggplot(some_disk_values, mo = "Escherichia coli", ab = "cipro") + ggplot(some_rsi_values) }diff --git a/docs/reference/random.html b/docs/reference/random.html index 3f67558d..fabdb0a0 100644 --- a/docs/reference/random.html +++ b/docs/reference/random.html @@ -82,7 +82,7 @@ diff --git a/docs/survey.html b/docs/survey.html index 87e4af3a..1d89b89d 100644 --- a/docs/survey.html +++ b/docs/survey.html @@ -81,7 +81,7 @@ diff --git a/man/ggplot_rsi.Rd b/man/ggplot_rsi.Rd index 1cc70659..797b5a17 100644 --- a/man/ggplot_rsi.Rd +++ b/man/ggplot_rsi.Rd @@ -53,11 +53,7 @@ facet_rsi(facet = c("interpretation", "antibiotic"), nrow = NULL) scale_y_percent(breaks = seq(0, 1, 0.1), limits = NULL) -scale_rsi_colours( - colours = c(S = "#3CAEA3", SI = "#3CAEA3", I = "#F6D55C", IR = "#ED553B", R = - "#ED553B"), - aesthetics = "fill" -) +scale_rsi_colours(..., aesthetics = "fill") theme_rsi() @@ -100,7 +96,7 @@ labels_rsi_count( \item{nrow}{(when using \code{facet}) number of rows} -\item{colours}{a named vector with colours for the bars. The names must be one or more of: S, SI, I, IR, R or be \code{FALSE} for standard \link[ggplot2:ggplot]{ggplot2} colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red.} +\item{colours}{a named vactor with colour to be used for filling. The default colours are colour-blind friendly.} \item{datalabels}{show datalabels using \code{\link[=labels_rsi_count]{labels_rsi_count()}}} @@ -118,9 +114,9 @@ labels_rsi_count( \item{y.title}{text to show as y axis description} -\item{...}{other arguments passed on to \code{\link[=geom_rsi]{geom_rsi()}}} +\item{...}{other arguments passed on to \code{\link[=geom_rsi]{geom_rsi()}} or, in case of \code{\link[=scale_rsi_colours]{scale_rsi_colours()}}, named values to set colours. The default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red. See \emph{Examples}.} -\item{aesthetics}{aesthetics to apply the colours to, defaults to "fill" but can also be "colour" or "both"} +\item{aesthetics}{aesthetics to apply the colours to, defaults to "fill" but can also be (a combination of) "alpha", "colour", "fill", "linetype", "shape" or "size"} } \description{ Use these functions to create bar plots for AMR data analysis. All functions rely on \link[ggplot2:ggplot]{ggplot2} functions. @@ -135,7 +131,7 @@ At default, the names of antibiotics will be shown on the plots using \code{\lin \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()}}. -\code{\link[=scale_rsi_colours]{scale_rsi_colours()}} sets colours to the bars: pastel blue for S, pastel turquoise for I and pastel red for R, using \code{\link[ggplot2:scale_manual]{ggplot2::scale_fill_manual()}}. +\code{\link[=scale_rsi_colours]{scale_rsi_colours()}} sets colours to the bars (green for S, yellow for I, and red for R). with 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. \code{\link[=theme_rsi]{theme_rsi()}} is a [ggplot2 theme][\code{\link[ggplot2:theme]{ggplot2::theme()}} with minimal distraction. diff --git a/man/plot.Rd b/man/plot.Rd index d8b95d10..9d6b010c 100644 --- a/man/plot.Rd +++ b/man/plot.Rd @@ -7,6 +7,7 @@ \alias{plot.disk} \alias{ggplot.disk} \alias{plot.rsi} +\alias{ggplot.rsi} \title{Plotting for Classes \code{rsi}, \code{mic} and \code{disk}} \usage{ \method{plot}{mic}( @@ -38,7 +39,7 @@ \method{plot}{disk}( x, - main = paste("Disk zones values of", deparse(substitute(x))), + main = paste("Disk zones of", deparse(substitute(x))), ylab = "Frequency", xlab = "Disk diffusion diameter (mm)", mo = NULL, @@ -52,7 +53,7 @@ \method{ggplot}{disk}( data, mapping = NULL, - title = paste("Disk zones values of", deparse(substitute(data))), + title = paste("Disk zones of", deparse(substitute(data))), ylab = "Frequency", xlab = "Disk diffusion diameter (mm)", mo = NULL, @@ -70,6 +71,16 @@ main = paste("Resistance Overview of", deparse(substitute(x))), ... ) + +\method{ggplot}{rsi}( + data, + mapping = NULL, + title = paste("Resistance Overview of", deparse(substitute(data))), + xlab = "Antimicrobial Interpretation", + ylab = "Frequency", + colours_RSI = c("#ED553B", "#3CAEA3", "#F6D55C"), + ... +) } \arguments{ \item{x, data}{MIC values created with \code{\link[=as.mic]{as.mic()}} or disk diffusion values created with \code{\link[=as.disk]{as.disk()}}} @@ -121,9 +132,11 @@ On our website \url{https://msberends.github.io/AMR/} you can find \href{https:/ \examples{ some_mic_values <- random_mic(size = 100) some_disk_values <- random_disk(size = 100, mo = "Escherichia coli", ab = "cipro") +some_rsi_values <- random_rsi(50, prob_RSI = c(0.30, 0.55, 0.05)) plot(some_mic_values) plot(some_disk_values) +plot(some_rsi_values) # when providing the microorganism and antibiotic, colours will show interpretations: plot(some_mic_values, mo = "S. aureus", ab = "ampicillin") @@ -132,5 +145,6 @@ plot(some_disk_values, mo = "Escherichia coli", ab = "cipro") if (require("ggplot2")) { ggplot(some_mic_values) ggplot(some_disk_values, mo = "Escherichia coli", ab = "cipro") + ggplot(some_rsi_values) } } diff --git a/pkgdown/extra.css b/pkgdown/extra.css index a0e2a435..093e035c 100644 --- a/pkgdown/extra.css +++ b/pkgdown/extra.css @@ -207,10 +207,11 @@ div[id^=last-updated] h2 { .table thead th { text-align: inherit; } -.table a:not(.btn) { +/* all tables, including argument lists */ +table a:not(.btn) { text-decoration: inherit; } -.table a:not(.btn):hover { +table a:not(.btn):hover { text-decoration: underline; } diff --git a/tests/testthat/test-rsi.R b/tests/testthat/test-rsi.R index 7cc41bc9..2de92259 100644 --- a/tests/testthat/test-rsi.R +++ b/tests/testthat/test-rsi.R @@ -42,6 +42,7 @@ test_that("rsi works", { pdf(NULL) # prevent Rplots.pdf being created expect_silent(barplot(as.rsi(c("S", "I", "R")))) expect_silent(plot(as.rsi(c("S", "I", "R")))) + if (require("ggplot2")) expect_s3_class(ggplot(as.rsi(c("S", "I", "R"))), "gg") expect_output(print(as.rsi(c("S", "I", "R")))) expect_equal(as.character(as.rsi(c(1:3))), c("S", "I", "R")) diff --git a/vignettes/AMR.Rmd b/vignettes/AMR.Rmd index a9b03f2c..d8469160 100755 --- a/vignettes/AMR.Rmd +++ b/vignettes/AMR.Rmd @@ -510,7 +510,7 @@ mic_values <- random_mic(size = 100) mic_values ``` -```{r} +```{r mic_plots} # base R: plot(mic_values) # ggplot2: @@ -520,39 +520,36 @@ ggplot(mic_values) But we could also be more specific, by generating MICs that are likely to be found in *E. coli* for ciprofloxacin: ```{r, results = 'markup', message = FALSE, warning = FALSE} -# this will generate MICs that are likely to be found in E. coli for ciprofloxacin: -mic_values_specific <- random_mic(size = 100, mo = "E. coli", ab = "cipro") +mic_values <- random_mic(size = 100, mo = "E. coli", ab = "cipro") ``` For the `plot()` and `ggplot()` function, we can define the microorganism and an antimicrobial agent the same way. This will add the interpretation of those values according to a chosen guidelines (defaults to the latest EUCAST guideline). Default colours are colour-blind friendly, while maintaining the convention that e.g. 'susceptible' should be green and 'resistant' should be red: -```{r, message = FALSE, warning = FALSE} +```{r mic_plots_mo_ab, message = FALSE, warning = FALSE} # base R: -plot(mic_values_specific, mo = "E. coli", ab = "cipro") +plot(mic_values, mo = "E. coli", ab = "cipro") # ggplot2: -ggplot(mic_values_specific, mo = "E. coli", ab = "cipro") +ggplot(mic_values, mo = "E. coli", ab = "cipro") ``` For disk diffusion values, there is not much of a difference in plotting: ```{r, results = 'markup'} -# this will generate disks that are likely to be found in E. coli for ciprofloxacin: -disk_values_specific <- random_disk(size = 100, mo = "E. coli", ab = "cipro") -disk_values_specific +disk_values <- random_disk(size = 100, mo = "E. coli", ab = "cipro") +disk_values ``` -```{r, message = FALSE, warning = FALSE} +```{r disk_plots, message = FALSE, warning = FALSE} # base R: -plot(disk_values_specific, mo = "E. coli", ab = "cipro") +plot(disk_values, mo = "E. coli", ab = "cipro") ``` And when using the `ggplot2` package, but now choosing the latest implemented CLSI guideline (notice that the EUCAST-specific term "Incr. exposure" has changed to "Intermediate"): -```{r, message = FALSE, warning = FALSE} -# and ggplot2, but now choosing an old CLSI guideline: -ggplot(disk_values_specific, +```{r disk_plots_mo_ab, message = FALSE, warning = FALSE} +ggplot(disk_values, mo = "E. coli", ab = "cipro", guideline = "CLSI")