mirror of
https://github.com/msberends/AMR.git
synced 2025-07-09 15:01:51 +02:00
(v1.7.1.9013) temp fix for ggplot2 bug #4511
This commit is contained in:
@ -30,7 +30,7 @@
|
||||
#' @param ab_class an antimicrobial class, such as `"carbapenems"`. The columns `group`, `atc_group1` and `atc_group2` of the [antibiotics] data set will be searched (case-insensitive) for this value.
|
||||
#' @param only_rsi_columns a [logical] to indicate whether only columns of class `<rsi>` must be selected (defaults to `FALSE`), see [as.rsi()]
|
||||
#' @details
|
||||
#' These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the [Tidyverse selection helpers](https://tidyselect.r-lib.org/reference/language.html), but also work in base \R and not only in `dplyr` verbs. Nonetheless, they are very convenient to use with `dplyr` functions such as [`select()`][dplyr::select()], [`filter()`][dplyr::filter()] and [`summarise()`][dplyr::summarise()], see *Examples*.
|
||||
#' These functions can be used in data set calls for selecting columns and filtering rows. They are heavily inspired by the [Tidyverse selection helpers][tidyselect::language] such as [`everything()`][tidyselect::everything()], but also work in base \R and not only in `dplyr` verbs. Nonetheless, they are very convenient to use with `dplyr` functions such as [`select()`][dplyr::select()], [`filter()`][dplyr::filter()] and [`summarise()`][dplyr::summarise()], see *Examples*.
|
||||
#'
|
||||
#' All columns in the data in which these functions are called will be searched for known antibiotic names, abbreviations, brand names, and codes (ATC, EARS-Net, WHO, etc.) in the [antibiotics] data set. This means that a selector such as [aminoglycosides()] will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc. Use the [ab_class()] function to filter/select on a manually defined antibiotic class.
|
||||
#'
|
||||
@ -124,149 +124,168 @@
|
||||
#' }
|
||||
ab_class <- function(ab_class,
|
||||
only_rsi_columns = FALSE) {
|
||||
meet_criteria(ab_class, allow_class = "character", has_length = 1)
|
||||
meet_criteria(ab_class, allow_class = "character", has_length = 1, allow_NULL = TRUE)
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector(NULL, only_rsi_columns = only_rsi_columns, ab_class = ab_class)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
aminoglycosides <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("aminoglycosides", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
aminopenicillins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("aminopenicillins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
betalactams <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("betalactams", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
carbapenems <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("carbapenems", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins_1st <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins_1st", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins_2nd <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins_2nd", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins_3rd <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins_3rd", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins_4th <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins_4th", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
cephalosporins_5th <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("cephalosporins_5th", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
fluoroquinolones <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("fluoroquinolones", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
glycopeptides <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("glycopeptides", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
lincosamides <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("lincosamides", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
lipoglycopeptides <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("lipoglycopeptides", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
macrolides <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("macrolides", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
oxazolidinones <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("oxazolidinones", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
penicillins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("penicillins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
polymyxins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("polymyxins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
streptogramins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("streptogramins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
quinolones <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("quinolones", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
tetracyclines <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("tetracyclines", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
#' @rdname antibiotic_class_selectors
|
||||
#' @export
|
||||
ureidopenicillins <- function(only_rsi_columns = FALSE) {
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1)
|
||||
ab_selector("ureidopenicillins", only_rsi_columns = only_rsi_columns)
|
||||
}
|
||||
|
||||
ab_selector <- function(function_name,
|
||||
only_rsi_columns,
|
||||
ab_class = NULL) {
|
||||
meet_criteria(function_name, allow_class = "character", has_length = 1, allow_NULL = TRUE, .call_depth = 1)
|
||||
meet_criteria(only_rsi_columns, allow_class = "logical", has_length = 1, .call_depth = 1)
|
||||
meet_criteria(ab_class, allow_class = "character", has_length = 1, allow_NULL = TRUE, .call_depth = 1)
|
||||
|
||||
# get_current_data() has to run each time, for cases where e.g., filter() and select() are used in same call
|
||||
# but it only takes a couple of milliseconds
|
||||
vars_df <- get_current_data(arg_name = NA, call = -3)
|
||||
|
@ -29,6 +29,7 @@
|
||||
#' @inheritSection lifecycle Stable Lifecycle
|
||||
#' @param x any (vector of) text that can be coerced to a valid antibiotic code with [as.ab()]
|
||||
#' @param tolower a [logical] to indicate whether the first [character] of every output should be transformed to a lower case [character]. This will lead to e.g. "polymyxin B" and not "polymyxin b".
|
||||
#' @param snake_case a [logical] to indicate whether the names should be returned in so-called [snake case](https://en.wikipedia.org/wiki/Snake_case): in lower case and all spaces/slashes replaced with an underscore (`_`). This is useful for column renaming.
|
||||
#' @param property one of the column names of one of the [antibiotics] data set
|
||||
#' @param language language of the returned text, defaults to system language (see [get_locale()]) and can also be set with `getOption("AMR_locale")`. Use `language = NULL` or `language = ""` to prevent translation.
|
||||
#' @param administration way of administration, either `"oral"` or `"iv"`
|
||||
@ -88,10 +89,11 @@
|
||||
#' ab_atc("cephtriaxone")
|
||||
#' ab_atc("cephthriaxone")
|
||||
#' ab_atc("seephthriaaksone")
|
||||
ab_name <- function(x, language = get_locale(), tolower = FALSE, ...) {
|
||||
ab_name <- function(x, language = get_locale(), tolower = FALSE, snake_case = FALSE, ...) {
|
||||
meet_criteria(x, allow_NA = TRUE)
|
||||
meet_criteria(language, has_length = 1, is_in = c(LANGUAGES_SUPPORTED, ""), allow_NULL = TRUE, allow_NA = TRUE)
|
||||
meet_criteria(tolower, allow_class = "logical", has_length = 1)
|
||||
meet_criteria(snake_case, allow_class = "logical", has_length = 1)
|
||||
|
||||
x <- translate_AMR(ab_validate(x = x, property = "name", ...), language = language, only_affect_ab_names = TRUE)
|
||||
if (tolower == TRUE) {
|
||||
@ -99,6 +101,9 @@ ab_name <- function(x, language = get_locale(), tolower = FALSE, ...) {
|
||||
# as we want "polymyxin B", not "polymyxin b"
|
||||
x <- gsub("^([A-Z])", "\\L\\1", x, perl = TRUE)
|
||||
}
|
||||
if (snake_case == TRUE) {
|
||||
x <- tolower(gsub("[^a-zA-Z0-9]+", "_", x))
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
|
@ -370,7 +370,6 @@ scale_rsi_colours <- function(...,
|
||||
aesthetics = "fill") {
|
||||
stop_ifnot_installed("ggplot2")
|
||||
meet_criteria(aesthetics, allow_class = "character", is_in = c("alpha", "colour", "color", "fill", "linetype", "shape", "size"))
|
||||
|
||||
# behaviour until AMR pkg v1.5.0 and also when coming from ggplot_rsi()
|
||||
if ("colours" %in% names(list(...))) {
|
||||
original_cols <- c(S = "#3CAEA3",
|
||||
@ -379,7 +378,9 @@ scale_rsi_colours <- function(...,
|
||||
IR = "#ED553B",
|
||||
R = "#ED553B")
|
||||
colours <- replace(original_cols, names(list(...)$colours), list(...)$colours)
|
||||
return(ggplot2::scale_fill_manual(values = 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))
|
||||
}
|
||||
if (identical(unlist(list(...)), FALSE)) {
|
||||
return(invisible())
|
||||
@ -411,7 +412,9 @@ scale_rsi_colours <- function(...,
|
||||
dots[dots == "I"] <- "#F6D55C"
|
||||
dots[dots == "R"] <- "#ED553B"
|
||||
cols <- replace(original_cols, names(dots), dots)
|
||||
ggplot2::scale_discrete_manual(aesthetics = aesthetics, values = cols)
|
||||
# limits = force is needed in ggplot2 3.3.4 and 3.3.5, see here;
|
||||
# https://github.com/tidyverse/ggplot2/issues/4511#issuecomment-866185530
|
||||
ggplot2::scale_discrete_manual(aesthetics = aesthetics, values = cols, limits = force)
|
||||
}
|
||||
|
||||
#' @rdname ggplot_rsi
|
||||
|
12
R/plot.R
12
R/plot.R
@ -276,8 +276,10 @@ ggplot.mic <- function(data,
|
||||
names(vals) <- translate_AMR(names(vals), language = language)
|
||||
p <- p +
|
||||
ggplot2::geom_col(ggplot2::aes(x = mic, y = count, fill = cols)) +
|
||||
# limits = force is needed because of a ggplot2 >= 3.3.4 bug (#4511)
|
||||
ggplot2::scale_fill_manual(values = vals,
|
||||
name = NULL)
|
||||
name = NULL,
|
||||
limits = force)
|
||||
} else {
|
||||
p <- p +
|
||||
ggplot2::geom_col(ggplot2::aes(x = mic, y = count))
|
||||
@ -500,8 +502,10 @@ ggplot.disk <- function(data,
|
||||
names(vals) <- translate_AMR(names(vals), language = language)
|
||||
p <- p +
|
||||
ggplot2::geom_col(ggplot2::aes(x = disk, y = count, fill = cols)) +
|
||||
# limits = force is needed because of a ggplot2 >= 3.3.4 bug (#4511)
|
||||
ggplot2::scale_fill_manual(values = vals,
|
||||
name = NULL)
|
||||
name = NULL,
|
||||
limits = force)
|
||||
} else {
|
||||
p <- p +
|
||||
ggplot2::geom_col(ggplot2::aes(x = disk, y = count))
|
||||
@ -660,9 +664,11 @@ ggplot.rsi <- function(data,
|
||||
|
||||
p +
|
||||
ggplot2::geom_col(ggplot2::aes(x = rsi, y = count, fill = rsi)) +
|
||||
# limits = force is needed because of a ggplot2 >= 3.3.4 bug (#4511)
|
||||
ggplot2::scale_fill_manual(values = c("R" = colours_RSI[1],
|
||||
"S" = colours_RSI[2],
|
||||
"I" = colours_RSI[3])) +
|
||||
"I" = colours_RSI[3]),
|
||||
limits = force) +
|
||||
ggplot2::labs(title = title, x = xlab, y = ylab) +
|
||||
ggplot2::theme(legend.position = "none")
|
||||
}
|
||||
|
Reference in New Issue
Block a user