mirror of
https://github.com/msberends/AMR.git
synced 2024-12-25 18:06:12 +01:00
ggplot2_rsi fix
This commit is contained in:
parent
cb0d74a4f0
commit
d049ec9e69
@ -20,7 +20,7 @@
|
||||
#'
|
||||
#' 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{"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"}, \code{"stack"} (default when \code{fun} is \code{\link{portion_df}}) or \code{"dodge"} (default when \code{fun} is \code{\link{count_df}})
|
||||
#' @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
|
||||
@ -56,7 +56,6 @@
|
||||
#' df <- septic_patients[, c("amox", "nitr", "fosf", "trim", "cipr")]
|
||||
#' ggplot(df) +
|
||||
#' geom_rsi() +
|
||||
#' facet_rsi() +
|
||||
#' scale_y_percent() +
|
||||
#' scale_rsi_colours() +
|
||||
#' theme_rsi()
|
||||
@ -87,12 +86,12 @@
|
||||
#' ggplot_rsi() + scale_fill_viridis_d()
|
||||
#'
|
||||
#'
|
||||
#' # it also supports groups (don't forget to use the group on `x` or `facet`):
|
||||
#' # it also supports groups (don't forget to use the group var on `x` or `facet`):
|
||||
#' septic_patients %>%
|
||||
#' select(hospital_id, amox, nitr, fosf, trim, cipr) %>%
|
||||
#' group_by(hospital_id) %>%
|
||||
#' ggplot_rsi(x = "hospital_id",
|
||||
#' facet = "Antibiotic",
|
||||
#' ggplot_rsi(x = hospital_id,
|
||||
#' facet = Antibiotic,
|
||||
#' nrow = 1) +
|
||||
#' labs(title = "AMR of Anti-UTI Drugs Per Hospital",
|
||||
#' x = "Hospital")
|
||||
@ -101,8 +100,8 @@
|
||||
#' septic_patients %>%
|
||||
#' # create new bacterial ID's, with all CoNS under the same group (Becker et al.)
|
||||
#' mutate(mo = as.mo(mo, Becker = TRUE)) %>%
|
||||
#' # filter on top 2 bacterial ID's
|
||||
#' filter(mo %in% top_freq(freq(.$mo), 2)) %>%
|
||||
#' # filter on top three bacterial ID's
|
||||
#' filter(mo %in% top_freq(freq(.$mo), 3)) %>%
|
||||
#' # determine first isolates
|
||||
#' mutate(first_isolate = first_isolate(.,
|
||||
#' col_date = "date",
|
||||
@ -110,17 +109,18 @@
|
||||
#' col_mo = "mo")) %>%
|
||||
#' # filter on first isolates
|
||||
#' filter(first_isolate == TRUE) %>%
|
||||
#' # join the `microorganisms` data set
|
||||
#' left_join_microorganisms() %>%
|
||||
#' # select full name and some antiseptic drugs
|
||||
#' select(mo = fullname,
|
||||
#' cfur, gent, cipr) %>%
|
||||
#' # get short MO names (like "E. coli")
|
||||
#' mutate(mo = mo_shortname(mo, Becker = TRUE)) %>%
|
||||
#' # select this short name and some antiseptic drugs
|
||||
#' select(mo, cfur, gent, cipr) %>%
|
||||
#' # group by MO
|
||||
#' group_by(mo) %>%
|
||||
#' # plot the thing, putting MOs on the facet
|
||||
#' ggplot_rsi(x = "Antibiotic",
|
||||
#' facet = "mo") +
|
||||
#' labs(title = "AMR of Top Two Microorganisms In Blood Culture Isolates",
|
||||
#' ggplot_rsi(x = Antibiotic,
|
||||
#' facet = mo,
|
||||
#' translate_ab = FALSE,
|
||||
#' nrow = 1) +
|
||||
#' labs(title = "AMR of Top Three Microorganisms In Blood Culture Isolates",
|
||||
#' subtitle = "Only First Isolates, CoNS grouped according to Becker et al. (2014)",
|
||||
#' x = "Microorganisms")
|
||||
#' }
|
||||
@ -144,6 +144,28 @@ ggplot_rsi <- function(data,
|
||||
stop("`fun` must be portion_df or count_df")
|
||||
}
|
||||
|
||||
x <- x[1]
|
||||
facet <- facet[1]
|
||||
|
||||
# we work with aes_string later on
|
||||
x_deparse <- deparse(substitute(x))
|
||||
if (x_deparse != "x") {
|
||||
x <- x_deparse
|
||||
}
|
||||
if (x %like% '".*"') {
|
||||
x <- substr(x, 2, nchar(x) - 1)
|
||||
}
|
||||
facet_deparse <- deparse(substitute(facet))
|
||||
if (facet_deparse != "facet") {
|
||||
facet <- facet_deparse
|
||||
}
|
||||
if (facet %like% '".*"') {
|
||||
facet <- substr(facet, 2, nchar(facet) - 1)
|
||||
}
|
||||
if (facet %in% c("NULL", "")) {
|
||||
facet <- NULL
|
||||
}
|
||||
|
||||
p <- ggplot2::ggplot(data = data) +
|
||||
geom_rsi(position = position, x = x, fill = fill, translate_ab = translate_ab, fun = fun, ...) +
|
||||
theme_rsi()
|
||||
@ -190,6 +212,16 @@ geom_rsi <- function(position = NULL,
|
||||
}
|
||||
|
||||
x <- x[1]
|
||||
|
||||
# we work with aes_string later on
|
||||
x_deparse <- deparse(substitute(x))
|
||||
if (x_deparse != "x") {
|
||||
x <- x_deparse
|
||||
}
|
||||
if (x %like% '".*"') {
|
||||
x <- substr(x, 2, nchar(x) - 1)
|
||||
}
|
||||
|
||||
if (tolower(x) %in% tolower(c('ab', 'antibiotic', 'abx', 'antibiotics'))) {
|
||||
x <- "Antibiotic"
|
||||
} else if (tolower(x) %in% tolower(c('SIR', 'RSI', 'interpretation', 'interpretations', 'result'))) {
|
||||
@ -208,7 +240,17 @@ geom_rsi <- function(position = NULL,
|
||||
#' @export
|
||||
facet_rsi <- function(facet = c("Interpretation", "Antibiotic"), nrow = NULL) {
|
||||
|
||||
facet <- facet[1]
|
||||
facet <- facet[1]
|
||||
|
||||
# we work with aes_string later on
|
||||
facet_deparse <- deparse(substitute(facet))
|
||||
if (facet_deparse != "facet") {
|
||||
facet <- facet_deparse
|
||||
}
|
||||
if (facet %like% '".*"') {
|
||||
facet <- substr(facet, 2, nchar(facet) - 1)
|
||||
}
|
||||
|
||||
if (tolower(facet) %in% tolower(c('SIR', 'RSI', 'interpretation', 'interpretations', 'result'))) {
|
||||
facet <- "Interpretation"
|
||||
} else if (tolower(facet) %in% tolower(c('ab', 'antibiotic', 'abx', 'antibiotics'))) {
|
||||
|
@ -142,12 +142,13 @@ mo_shortname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL)
|
||||
res2_fullname <- mo_fullname(res2)
|
||||
res2_fullname[res2_fullname %like% "\\(CoNS\\)"] <- "CoNS"
|
||||
res2_fullname[res2_fullname %like% "\\(CoPS\\)"] <- "CoPS"
|
||||
res2_fullname <- gsub("Streptococcus (group|gruppe|Gruppe|groep|grupo) (.)",
|
||||
res2_fullname <- gsub("Streptococcus (group|Gruppe|gruppe|groep|grupo|gruppo|groupe) (.)",
|
||||
"G\\2S",
|
||||
res2_fullname) # turn "Streptococcus group A" and "Streptococcus grupo A" to "GAS"
|
||||
res2_fullname[res2_fullname == mo_fullname(x)] <- paste0(substr(mo_genus(res2_fullname), 1, 1),
|
||||
res2_fullname_vector <- res2_fullname[res2_fullname == mo_fullname(x)]
|
||||
res2_fullname[res2_fullname == mo_fullname(x)] <- paste0(substr(mo_genus(res2_fullname_vector), 1, 1),
|
||||
". ",
|
||||
suppressWarnings(mo_species(res2_fullname)))
|
||||
suppressWarnings(mo_species(res2_fullname_vector)))
|
||||
if (sum(res1 == res2, na.rm = TRUE) > 0) {
|
||||
res1[res1 == res2] <- paste0(substr(mo_genus(res1[res1 == res2]), 1, 1),
|
||||
". ",
|
||||
|
@ -28,7 +28,7 @@ theme_rsi()
|
||||
\arguments{
|
||||
\item{data}{a \code{data.frame} with column(s) of class \code{"rsi"} (see \code{\link{as.rsi}})}
|
||||
|
||||
\item{position}{position adjustment of bars, either \code{"stack"} (default when \code{fun} is \code{\link{portion_df}}) or \code{"dodge"} (default when \code{fun} is \code{\link{count_df}})}
|
||||
\item{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}})}
|
||||
|
||||
\item{x}{variable to show on x axis, either \code{"Antibiotic"} (default) or \code{"Interpretation"} or a grouping variable}
|
||||
|
||||
@ -75,7 +75,6 @@ ggplot(septic_patients \%>\% select(amox, nitr, fosf, trim, cipr)) +
|
||||
df <- septic_patients[, c("amox", "nitr", "fosf", "trim", "cipr")]
|
||||
ggplot(df) +
|
||||
geom_rsi() +
|
||||
facet_rsi() +
|
||||
scale_y_percent() +
|
||||
scale_rsi_colours() +
|
||||
theme_rsi()
|
||||
@ -106,12 +105,12 @@ septic_patients \%>\%
|
||||
ggplot_rsi() + scale_fill_viridis_d()
|
||||
|
||||
|
||||
# it also supports groups (don't forget to use the group on `x` or `facet`):
|
||||
# it also supports groups (don't forget to use the group var on `x` or `facet`):
|
||||
septic_patients \%>\%
|
||||
select(hospital_id, amox, nitr, fosf, trim, cipr) \%>\%
|
||||
group_by(hospital_id) \%>\%
|
||||
ggplot_rsi(x = "hospital_id",
|
||||
facet = "Antibiotic",
|
||||
ggplot_rsi(x = hospital_id,
|
||||
facet = Antibiotic,
|
||||
nrow = 1) +
|
||||
labs(title = "AMR of Anti-UTI Drugs Per Hospital",
|
||||
x = "Hospital")
|
||||
@ -120,8 +119,8 @@ septic_patients \%>\%
|
||||
septic_patients \%>\%
|
||||
# create new bacterial ID's, with all CoNS under the same group (Becker et al.)
|
||||
mutate(mo = as.mo(mo, Becker = TRUE)) \%>\%
|
||||
# filter on top 2 bacterial ID's
|
||||
filter(mo \%in\% top_freq(freq(.$mo), 2)) \%>\%
|
||||
# filter on top three bacterial ID's
|
||||
filter(mo \%in\% top_freq(freq(.$mo), 3)) \%>\%
|
||||
# determine first isolates
|
||||
mutate(first_isolate = first_isolate(.,
|
||||
col_date = "date",
|
||||
@ -129,17 +128,18 @@ septic_patients \%>\%
|
||||
col_mo = "mo")) \%>\%
|
||||
# filter on first isolates
|
||||
filter(first_isolate == TRUE) \%>\%
|
||||
# join the `microorganisms` data set
|
||||
left_join_microorganisms() \%>\%
|
||||
# select full name and some antiseptic drugs
|
||||
select(mo = fullname,
|
||||
cfur, gent, cipr) \%>\%
|
||||
# get short MO names (like "E. coli")
|
||||
mutate(mo = mo_shortname(mo, Becker = TRUE)) \%>\%
|
||||
# select this short name and some antiseptic drugs
|
||||
select(mo, cfur, gent, cipr) \%>\%
|
||||
# group by MO
|
||||
group_by(mo) \%>\%
|
||||
# plot the thing, putting MOs on the facet
|
||||
ggplot_rsi(x = "Antibiotic",
|
||||
facet = "mo") +
|
||||
labs(title = "AMR of Top Two Microorganisms In Blood Culture Isolates",
|
||||
ggplot_rsi(x = Antibiotic,
|
||||
facet = mo,
|
||||
translate_ab = FALSE,
|
||||
nrow = 1) +
|
||||
labs(title = "AMR of Top Three Microorganisms In Blood Culture Isolates",
|
||||
subtitle = "Only First Isolates, CoNS grouped according to Becker et al. (2014)",
|
||||
x = "Microorganisms")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user