mirror of
https://github.com/msberends/AMR.git
synced 2025-07-08 09:11:51 +02:00
add labels_rsi_count
This commit is contained in:
@ -36,7 +36,7 @@
|
||||
#' ab_umcg("amcl") # AMCL
|
||||
ab_property <- function(x, property = 'official') {
|
||||
property <- property[1]
|
||||
if (!property %in% colnames(antibiotics)) {
|
||||
if (!property %in% colnames(AMR::antibiotics)) {
|
||||
stop("invalid property: ", property, " - use a column name of the `antibiotics` data set")
|
||||
}
|
||||
if (!is.atc(x)) {
|
||||
|
@ -146,6 +146,10 @@ count_S <- function(...) {
|
||||
count_df <- function(data,
|
||||
translate_ab = getOption("get_antibiotic_names", "official")) {
|
||||
|
||||
if (!"data.frame" %in% class(data)) {
|
||||
stop("`count_df` must be called on a data.frame")
|
||||
}
|
||||
|
||||
if (data %>% select_if(is.rsi) %>% ncol() == 0) {
|
||||
stop("No columns with class 'rsi' found. See ?as.rsi.")
|
||||
}
|
||||
@ -177,7 +181,7 @@ count_df <- function(data,
|
||||
|
||||
res <- bind_rows(resS, resI, resR) %>%
|
||||
mutate(Interpretation = factor(Interpretation, levels = c("R", "I", "S"), ordered = TRUE)) %>%
|
||||
tidyr::gather(Antibiotic, Count, -Interpretation, -data.groups)
|
||||
tidyr::gather(Antibiotic, Value, -Interpretation, -data.groups)
|
||||
|
||||
if (!translate_ab == FALSE) {
|
||||
if (!tolower(translate_ab) %in% tolower(colnames(AMR::antibiotics))) {
|
||||
|
@ -20,13 +20,16 @@
|
||||
#'
|
||||
#' Use these functions to create bar plots for antimicrobial resistance analysis. All functions rely on internal \code{\link[ggplot2]{ggplot}} functions.
|
||||
#' @param data a \code{data.frame} with column(s) of class \code{"rsi"} (see \code{\link{as.rsi}})
|
||||
#' @param position position adjustment of bars, either \code{"fill"}, \code{"stack"} (default when \code{fun} is \code{\link{portion_df}}) or \code{"dodge"} (default when \code{fun} is \code{\link{count_df}})
|
||||
#' @param position position adjustment of bars, either \code{"fill"} (default when \code{fun} is \code{\link{count_df}}), \code{"stack"} (default when \code{fun} is \code{\link{portion_df}}) or \code{"dodge"}
|
||||
#' @param x variable to show on x axis, either \code{"Antibiotic"} (default) or \code{"Interpretation"} or a grouping variable
|
||||
#' @param fill variable to categorise using the plots legend, either \code{"Antibiotic"} (default) or \code{"Interpretation"} or a grouping variable
|
||||
#' @param facet variable to split plots by, either \code{"Interpretation"} (default) or \code{"Antibiotic"} or a grouping variable
|
||||
#' @param translate_ab a column name of the \code{\link{antibiotics}} data set to translate the antibiotic abbreviations into, using \code{\link{abname}}. Default behaviour is to translate to official names according to the WHO. Use \code{translate_ab = FALSE} to disable translation.
|
||||
#' @param fun function to transform \code{data}, either \code{\link{portion_df}} (default) or \code{\link{count_df}}
|
||||
#' @param fun function to transform \code{data}, either \code{\link{count_df}} (default) or \code{\link{portion_df}}
|
||||
#' @param nrow (when using \code{facet}) number of rows
|
||||
#' @param datalabels show datalabels using \code{labels_rsi_count}, will at default only be shown when \code{fun = count_df}
|
||||
#' @param datalabels.size size of the datalabels
|
||||
#' @param datalabels.colour colour of the datalabels
|
||||
#' @param ... other parameters passed on to \code{geom_rsi}
|
||||
#' @details At default, the names of antibiotics will be shown on the plots using \code{\link{abname}}. This can be set with the option \code{get_antibiotic_names} (a logical value), so change it e.g. to \code{FALSE} with \code{options(get_antibiotic_names = FALSE)}.
|
||||
#'
|
||||
@ -35,12 +38,14 @@
|
||||
#'
|
||||
#' \code{facet_rsi} creates 2d plots (at default based on S/I/R) using \code{\link[ggplot2]{facet_wrap}}.
|
||||
#'
|
||||
#' \code{scale_y_percent} transforms the y axis to a 0 to 100\% range.
|
||||
#' \code{scale_y_percent} transforms the y axis to a 0 to 100\% range using \code{\link[ggplot2]{scale_y_continuous}}.
|
||||
#'
|
||||
#' \code{scale_rsi_colours} sets colours to the bars: green for S, yellow for I and red for R.
|
||||
#' \code{scale_rsi_colours} sets colours to the bars: green for S, yellow for I and red for R, using \code{\link[ggplot2]{scale_fill_brewer}}.
|
||||
#'
|
||||
#' \code{theme_rsi} is a \code{\link[ggplot2]{theme}} with minimal distraction.
|
||||
#'
|
||||
#' \code{labels_rsi_count} print datalabels on the bars with percentage and amount of isolates using \code{\link[ggplot2]{geom_text}}
|
||||
#'
|
||||
#' \code{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 (\code{\%>\%}). See Examples.
|
||||
#' @rdname ggplot_rsi
|
||||
#' @export
|
||||
@ -58,6 +63,7 @@
|
||||
#' geom_rsi() +
|
||||
#' scale_y_percent() +
|
||||
#' scale_rsi_colours() +
|
||||
#' labels_rsi_count() +
|
||||
#' theme_rsi()
|
||||
#'
|
||||
#' # or better yet, simplify this using the wrapper function - a single command:
|
||||
@ -131,8 +137,11 @@ ggplot_rsi <- function(data,
|
||||
# params = list(),
|
||||
facet = NULL,
|
||||
translate_ab = "official",
|
||||
fun = portion_df,
|
||||
fun = count_df,
|
||||
nrow = NULL,
|
||||
datalabels = TRUE,
|
||||
datalabels.size = 3,
|
||||
datalabels.colour = "grey15",
|
||||
...) {
|
||||
|
||||
if (!"ggplot2" %in% rownames(installed.packages())) {
|
||||
@ -174,11 +183,22 @@ ggplot_rsi <- function(data,
|
||||
# set RSI colours
|
||||
p <- p + scale_rsi_colours()
|
||||
}
|
||||
if (fun_name == "portion_df") {
|
||||
if (is.null(position)) {
|
||||
position <- "fill"
|
||||
}
|
||||
if (fun_name == "portion_df"
|
||||
| (fun_name == "count_df" & position == "fill")) {
|
||||
# portions, so use y scale with percentage
|
||||
p <- p + scale_y_percent()
|
||||
}
|
||||
|
||||
if (fun_name == "count_df" & datalabels == TRUE) {
|
||||
p <- p + labels_rsi_count(position = position,
|
||||
x = x,
|
||||
datalabels.size = datalabels.size,
|
||||
datalabels.colour = datalabels.colour)
|
||||
}
|
||||
|
||||
if (!is.null(facet)) {
|
||||
p <- p + facet_rsi(facet = facet, nrow = nrow)
|
||||
}
|
||||
@ -192,20 +212,19 @@ geom_rsi <- function(position = NULL,
|
||||
x = c("Antibiotic", "Interpretation"),
|
||||
fill = "Interpretation",
|
||||
translate_ab = "official",
|
||||
fun = portion_df,
|
||||
fun = count_df,
|
||||
...) {
|
||||
|
||||
fun_name <- deparse(substitute(fun))
|
||||
if (!fun_name %in% c("portion_df", "count_df", "fun")) {
|
||||
stop("`fun` must be portion_df or count_df")
|
||||
}
|
||||
y <- "Value"
|
||||
if (identical(fun, count_df)) {
|
||||
y <- "Count"
|
||||
if (missing(position) | is.null(position)) {
|
||||
position <- "dodge"
|
||||
position <- "fill"
|
||||
}
|
||||
} else {
|
||||
y <- "Percentage"
|
||||
if (missing(position) | is.null(position)) {
|
||||
position <- "stack"
|
||||
}
|
||||
@ -264,7 +283,6 @@ facet_rsi <- function(facet = c("Interpretation", "Antibiotic"), nrow = NULL) {
|
||||
#' @export
|
||||
scale_y_percent <- function() {
|
||||
ggplot2::scale_y_continuous(breaks = seq(0, 1, 0.1),
|
||||
limits = c(0, 1),
|
||||
labels = percent(seq(0, 1, 0.1)))
|
||||
}
|
||||
|
||||
@ -282,3 +300,35 @@ theme_rsi <- function() {
|
||||
panel.grid.minor = ggplot2::element_blank(),
|
||||
panel.grid.major.y = ggplot2::element_line(colour = "grey75"))
|
||||
}
|
||||
|
||||
#' @rdname ggplot_rsi
|
||||
#' @export
|
||||
labels_rsi_count <- function(position = NULL,
|
||||
x = "Antibiotic",
|
||||
datalabels.size = 3,
|
||||
datalabels.colour = "grey15") {
|
||||
if (is.null(position)) {
|
||||
position <- "fill"
|
||||
}
|
||||
if (position == "fill") {
|
||||
position <- ggplot2::position_fill(vjust = 0.5)
|
||||
}
|
||||
ggplot2::geom_text(mapping = ggplot2::aes_string(label = "lbl",
|
||||
x = x,
|
||||
y = "Value"),
|
||||
position = position,
|
||||
data = getlbls,
|
||||
inherit.aes = FALSE,
|
||||
size = datalabels.size,
|
||||
colour = datalabels.colour)
|
||||
}
|
||||
|
||||
#' @importFrom dplyr %>% group_by mutate
|
||||
getlbls <- function(data) {
|
||||
data %>%
|
||||
count_df() %>%
|
||||
group_by(Antibiotic) %>%
|
||||
mutate(lbl = paste0(percent(Value / sum(Value, na.rm = TRUE), force_zero = TRUE),
|
||||
" (n=", Value, ")")) %>%
|
||||
mutate(lbl = ifelse(lbl == "0.0% (n=0)", "", lbl))
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ globalVariables(c(".",
|
||||
"antibiotics",
|
||||
"cnt",
|
||||
"count",
|
||||
"Count",
|
||||
"cum_count",
|
||||
"cum_percent",
|
||||
"date_lab",
|
||||
@ -37,6 +36,7 @@ globalVariables(c(".",
|
||||
"key_ab",
|
||||
"key_ab_lag",
|
||||
"key_ab_other",
|
||||
"lbl",
|
||||
"median",
|
||||
"mic",
|
||||
"microorganisms",
|
||||
@ -46,7 +46,6 @@ globalVariables(c(".",
|
||||
"other_pat_or_mo",
|
||||
"Pasted",
|
||||
"patient_id",
|
||||
"Percentage",
|
||||
"prevalence",
|
||||
"R",
|
||||
"real_first_isolate",
|
||||
@ -54,4 +53,5 @@ globalVariables(c(".",
|
||||
"septic_patients",
|
||||
"species",
|
||||
"value",
|
||||
"Value",
|
||||
"y"))
|
||||
|
@ -187,7 +187,7 @@ mo_aerobic <- function(x) {
|
||||
#' @export
|
||||
mo_property <- function(x, property = 'fullname', Becker = FALSE, Lancefield = FALSE, language = NULL) {
|
||||
property <- tolower(property[1])
|
||||
if (!property %in% colnames(microorganisms)) {
|
||||
if (!property %in% colnames(AMR::microorganisms)) {
|
||||
stop("invalid property: ", property, " - use a column name of the `microorganisms` data set")
|
||||
}
|
||||
result1 <- as.mo(x = x, Becker = Becker, Lancefield = Lancefield) # this will give a warning if x cannot be coerced
|
||||
|
@ -203,6 +203,10 @@ portion_df <- function(data,
|
||||
minimum = 30,
|
||||
as_percent = FALSE) {
|
||||
|
||||
if (!"data.frame" %in% class(data)) {
|
||||
stop("`portion_df` must be called on a data.frame")
|
||||
}
|
||||
|
||||
if (data %>% select_if(is.rsi) %>% ncol() == 0) {
|
||||
stop("No columns with class 'rsi' found. See ?as.rsi.")
|
||||
}
|
||||
@ -240,7 +244,7 @@ portion_df <- function(data,
|
||||
|
||||
res <- bind_rows(resS, resI, resR) %>%
|
||||
mutate(Interpretation = factor(Interpretation, levels = c("R", "I", "S"), ordered = TRUE)) %>%
|
||||
tidyr::gather(Antibiotic, Percentage, -Interpretation, -data.groups)
|
||||
tidyr::gather(Antibiotic, Value, -Interpretation, -data.groups)
|
||||
|
||||
if (!translate_ab == FALSE) {
|
||||
if (!tolower(translate_ab) %in% tolower(colnames(AMR::antibiotics))) {
|
||||
|
Reference in New Issue
Block a user