mirror of
https://github.com/msberends/AMR.git
synced 2026-11-16 04:20:53 +01:00
(v3.1.0.9018) Italicise taxonomic names in all plotting functions using labels_taxonomy(), and fix ggplot_pca() with explicit groups
This commit is contained in:
1 parent
cfb55a4c3e
commit
77b6cb1713
10 files changed
+78
-11
No files matched your search
+3
-3
@@ -1404,7 +1404,7 @@ plot.antibiogram <- function(x, ...) {
|
||||
names.arg = df_sub$ab,
|
||||
col = "#aaaaaa",
|
||||
beside = TRUE,
|
||||
main = mo,
|
||||
main = labels_taxonomy(mo),
|
||||
legend = NULL
|
||||
)
|
||||
|
||||
@@ -1568,7 +1568,7 @@ autoplot.antibiogram <- function(object,
|
||||
}
|
||||
|
||||
if (has_facet) {
|
||||
out <- out + ggplot2::facet_wrap("mo")
|
||||
out <- out + ggplot2::facet_wrap("mo", labeller = labeller_taxonomy)
|
||||
}
|
||||
|
||||
out
|
||||
@@ -1733,6 +1733,7 @@ plot_wisca_susceptibility_incidence <- function(wisca_model, sep) {
|
||||
colour = translate_AMR("Pathogen", language = get_AMR_locale()),
|
||||
caption = paste(attributes(wisca_model)$simulations, "Monte Carlo simulations")
|
||||
) +
|
||||
ggplot2::scale_colour_discrete(labels = labels_taxonomy) +
|
||||
ggplot2::guides(
|
||||
colour = ggplot2::guide_legend(
|
||||
override.aes = list(alpha = 1, size = 3)
|
||||
@@ -1741,7 +1742,6 @@ plot_wisca_susceptibility_incidence <- function(wisca_model, sep) {
|
||||
ggplot2::theme(
|
||||
legend.position = "right",
|
||||
legend.key.spacing.y = ggplot2::unit(0.25, "lines"),
|
||||
legend.text = ggplot2::element_text(face = "italic"),
|
||||
plot.title = ggplot2::element_text(size = 12, face = "bold")
|
||||
)
|
||||
}
|
||||
|
||||
+24
-4
@@ -151,6 +151,7 @@ ggplot_pca <- function(x,
|
||||
pca_model = x,
|
||||
groups = groups,
|
||||
groups_missing = missing(groups),
|
||||
group_name = if (missing(groups)) NULL else paste(deparse(substitute(groups)), collapse = ""),
|
||||
labels = labels,
|
||||
labels_missing = missing(labels),
|
||||
choices = choices,
|
||||
@@ -183,8 +184,19 @@ ggplot_pca <- function(x,
|
||||
)
|
||||
|
||||
# Score Labels
|
||||
labels_parse <- FALSE
|
||||
if (!is.null(labels)) {
|
||||
df.u$labels <- labels
|
||||
if (contains_taxonomy(labels)) {
|
||||
# italicise taxonomic names, as plotmath text to be parsed by geom_text()
|
||||
labels_parse <- TRUE
|
||||
df.u$labels <- vapply(
|
||||
FUN.VALUE = character(1),
|
||||
as.list(labels_taxonomy(labels)),
|
||||
function(e) if (is.character(e) && is.na(e)) NA_character_ else paste(deparse(e, width.cutoff = 500L), collapse = ""),
|
||||
USE.NAMES = FALSE
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
# Grouping variable
|
||||
@@ -213,7 +225,8 @@ ggplot_pca <- function(x,
|
||||
) +
|
||||
ggplot2::geom_text(ggplot2::aes(label = labels, colour = groups),
|
||||
nudge_y = -0.05,
|
||||
size = labels_textsize
|
||||
size = labels_textsize,
|
||||
parse = labels_parse
|
||||
) +
|
||||
ggplot2::labs(colour = group_name)
|
||||
} else {
|
||||
@@ -223,7 +236,8 @@ ggplot_pca <- function(x,
|
||||
) +
|
||||
ggplot2::geom_text(ggplot2::aes(label = labels),
|
||||
nudge_y = -0.05,
|
||||
size = labels_textsize
|
||||
size = labels_textsize,
|
||||
parse = labels_parse
|
||||
)
|
||||
}
|
||||
} else {
|
||||
@@ -241,6 +255,11 @@ ggplot_pca <- function(x,
|
||||
}
|
||||
}
|
||||
|
||||
# italicise taxonomic names in the legend
|
||||
if (!is.null(df.u$groups) && contains_taxonomy(df.u$groups)) {
|
||||
g <- g + ggplot2::scale_colour_discrete(labels = labels_taxonomy)
|
||||
}
|
||||
|
||||
# Overlay a concentration ellipse if there are groups
|
||||
if (!is.null(df.u$groups) && !is.null(ell) && isTRUE(ellipse)) {
|
||||
g <- g + ggplot2::geom_path(
|
||||
@@ -308,6 +327,7 @@ ggplot_pca <- function(x,
|
||||
pca_calculations <- function(pca_model,
|
||||
groups = NULL,
|
||||
groups_missing = TRUE,
|
||||
group_name = NULL,
|
||||
labels = NULL,
|
||||
labels_missing = TRUE,
|
||||
choices = 1:2,
|
||||
@@ -329,8 +349,8 @@ pca_calculations <- function(pca_model,
|
||||
error = function(e) NULL
|
||||
)
|
||||
}
|
||||
if (!is.null(groups) && is.null(labels)) {
|
||||
# turn them around
|
||||
if (groups_missing && !is.null(groups) && is.null(labels)) {
|
||||
# only one non-numeric column: use it for labels instead of groups
|
||||
labels <- groups
|
||||
groups <- NULL
|
||||
group_name <- NULL
|
||||
|
||||
@@ -270,6 +270,14 @@ ggplot_sir <- function(data,
|
||||
)
|
||||
}
|
||||
|
||||
# italicise taxonomic names on the x axis and in the legend, e.g. with x = "mo" or fill = "mo"
|
||||
if (x %in% colnames(data) && contains_taxonomy(data[[x]])) {
|
||||
p <- p + ggplot2::scale_x_discrete(labels = labels_taxonomy)
|
||||
}
|
||||
if (fill != "interpretation" && fill %in% colnames(data) && contains_taxonomy(data[[fill]])) {
|
||||
p <- p + ggplot2::scale_fill_discrete(labels = labels_taxonomy)
|
||||
}
|
||||
|
||||
if (!is.null(facet)) {
|
||||
# pass the value, as facet_sir() takes an unquoted name as column name
|
||||
p <- p + do.call(facet_sir, list(facet = facet, nrow = nrow))
|
||||
|
||||
@@ -107,6 +107,12 @@ italicise_taxonomy <- function(string, type = c("markdown", "ansi", "html", "plo
|
||||
)
|
||||
}
|
||||
|
||||
contains_taxonomy <- function(x) {
|
||||
# whether any value holds a taxonomic name that would be italicised
|
||||
x <- unique(as.character(x))
|
||||
any(vapply(taxonomy_pieces(x[!is.na(x)]), function(p) any(p$italic), logical(1)))
|
||||
}
|
||||
|
||||
get_taxonomy_names <- function() {
|
||||
# cached for the session, reset by reset_mo_cache() when the lookup table changes
|
||||
if (is.null(AMR_env$taxonomy_names)) {
|
||||
|
||||
+15
-2
@@ -80,6 +80,8 @@
|
||||
#'
|
||||
#' The function [labels_taxonomy()] prints taxonomic names in axis and legend labels in italics, as is the convention in microbiology, e.g., *E. coli* and *Klebsiella pneumoniae*. It can directly be used as labeller in `ggplot2`, e.g., `scale_x_discrete(labels = labels_taxonomy)`, and in base \R, e.g., `axis(1, at = 1:3, labels = labels_taxonomy(names))`. Other words, such as "Other" or "CoNS", stay upright. It uses [italicise_taxonomy()] with `type = "plotmath"`, which also works for titles. It returns an [expression] vector of the same length as `x`; other arguments in `...` are ignored.
|
||||
#'
|
||||
#' The plotting functions of this package apply this automatically wherever they show taxonomic names, e.g., in the facets and titles of [antibiogram()] plots, in the legend of [wisca_plot()], on the axes, legends and facets of [ggplot_sir()] and [facet_sir()], in [ggplot_pca()], and in the subtitles of MIC and disk diffusion plots.
|
||||
#'
|
||||
#' The interpretation of "I" will be named "Susceptible, incr. exp." (susceptible, increased exposure) for all EUCAST guidelines since 2019, and will be named "Intermediate" in all other cases.
|
||||
#'
|
||||
#' For interpreting MIC values as well as disk diffusion diameters, the default guideline is `r AMR::clinical_breakpoints$guideline[1]`, unless the package option [`AMR_guideline`][AMR-options] is set. See [as.sir()] for more information.
|
||||
@@ -1288,7 +1290,7 @@ plot_colours_subtitle_guideline <- function(x, mo, ab, guideline, colours_SIR, f
|
||||
sir <- as.character(sir)
|
||||
cols <- unname(plot_sir_colour_values(colours_SIR)[sir])
|
||||
cols[is.na(cols)] <- "#BEBEBE"
|
||||
sub <- bquote(.(abname) ~ "-" ~ italic(.(moname)) ~ .(guideline_txt))
|
||||
sub <- bquote(.(abname) ~ "-" ~ .(italicise_taxonomy(moname, type = "plotmath")[[1]]) ~ .(guideline_txt))
|
||||
} else {
|
||||
sir <- rep(NA_character_, length(x))
|
||||
cols <- "#BEBEBE"
|
||||
@@ -1319,7 +1321,7 @@ facet_sir <- function(facet = c("interpretation", "antibiotic"), nrow = NULL) {
|
||||
facet <- "antibiotic"
|
||||
}
|
||||
|
||||
ggplot2::facet_wrap(facets = facet, scales = "free_x", nrow = nrow)
|
||||
ggplot2::facet_wrap(facets = facet, scales = "free_x", nrow = nrow, labeller = labeller_taxonomy)
|
||||
}
|
||||
|
||||
#' @rdname plot
|
||||
@@ -1437,6 +1439,17 @@ labels_taxonomy <- function(x, ...) {
|
||||
italicise_taxonomy(as.character(x), type = "plotmath")
|
||||
}
|
||||
|
||||
# ggplot2 facet labeller that italicises taxonomic names in the strips
|
||||
labeller_taxonomy <- structure(
|
||||
function(labels) {
|
||||
lapply(labels, function(values) {
|
||||
expr <- labels_taxonomy(values)
|
||||
lapply(seq_along(expr), function(i) expr[i])
|
||||
})
|
||||
},
|
||||
class = c("function", "labeller")
|
||||
)
|
||||
|
||||
#' @rdname ggplot_sir
|
||||
#' @export
|
||||
labels_sir_count <- function(position = NULL,
|
||||
|
||||
Reference in new issue
Block a user