mirror of
https://github.com/msberends/AMR.git
synced 2026-11-16 03:40:48 +01:00
(v3.1.0.9019) Fix the data labels of labels_sir_count() being placed on the wrong bars with position = "dodge" or "stack"
This commit is contained in:
1 parent
77b6cb1713
commit
054066b855
6 files changed
+37
-5
No files matched your search
+2
-1
@@ -33,7 +33,7 @@
|
||||
#' @param data A [data.frame] with column(s) of class [`sir`] (see [as.sir()]).
|
||||
#' @param position Position adjustment of bars, either `"fill"`, `"stack"` or `"dodge"`.
|
||||
#' @param x Variable to show on x axis, either `"antibiotic"` (default) or `"interpretation"` or a grouping variable.
|
||||
#' @param fill Variable to categorise using the plots legend, either `"antibiotic"` (default) or `"interpretation"` or a grouping variable.
|
||||
#' @param fill Variable to categorise using the plots legend, either `"interpretation"` (default) or `"antibiotic"` or a grouping variable. For [labels_sir_count()], this must be the same as the `fill` of [geom_sir()], so that the labels are placed on the right bars.
|
||||
#' @param breaks A [numeric] vector of positions.
|
||||
#' @param limits A [numeric] vector of length two providing limits of the scale, use `NA` to refer to the existing minimum or maximum.
|
||||
#' @param facet Variable to split plots by, either `"interpretation"` (default) or `"antibiotic"` or a grouping variable.
|
||||
@@ -261,6 +261,7 @@ ggplot_sir <- function(data,
|
||||
p <- p + labels_sir_count(
|
||||
position = position,
|
||||
x = x,
|
||||
fill = fill,
|
||||
translate_ab = translate_ab,
|
||||
minimum = minimum,
|
||||
language = language,
|
||||
|
||||
+14
-1
@@ -1454,6 +1454,7 @@ labeller_taxonomy <- structure(
|
||||
#' @export
|
||||
labels_sir_count <- function(position = NULL,
|
||||
x = "antibiotic",
|
||||
fill = "interpretation",
|
||||
translate_ab = "name",
|
||||
minimum = 30,
|
||||
language = get_AMR_locale(),
|
||||
@@ -1464,6 +1465,7 @@ labels_sir_count <- function(position = NULL,
|
||||
stop_ifnot_installed("ggplot2")
|
||||
meet_criteria(position, allow_class = "character", has_length = 1, is_in = c("fill", "stack", "dodge"), allow_NULL = TRUE)
|
||||
meet_criteria(x, allow_class = "character", has_length = 1)
|
||||
meet_criteria(fill, allow_class = "character", has_length = 1)
|
||||
meet_criteria(translate_ab, allow_class = c("character", "logical"), has_length = 1, allow_NA = TRUE)
|
||||
meet_criteria(minimum, allow_class = c("numeric", "integer"), has_length = 1, is_positive_or_zero = TRUE, is_finite = TRUE)
|
||||
language <- validate_language(language)
|
||||
@@ -1474,18 +1476,29 @@ labels_sir_count <- function(position = NULL,
|
||||
if (is.null(position)) {
|
||||
position <- "fill"
|
||||
}
|
||||
# labels are grouped like the bars of geom_sir(), so that they are placed on the right bar
|
||||
vjust <- 0.5
|
||||
if (identical(position, "fill")) {
|
||||
# centred in the bar segments
|
||||
position <- ggplot2::position_fill(vjust = 0.5, reverse = TRUE)
|
||||
} else if (identical(position, "stack")) {
|
||||
# centred in the bar segments
|
||||
position <- ggplot2::position_stack(vjust = 0.5)
|
||||
} else if (identical(position, "dodge")) {
|
||||
# on top of the bars, with the default width of geom_col()
|
||||
position <- ggplot2::position_dodge(width = 0.9)
|
||||
vjust <- -0.5
|
||||
}
|
||||
|
||||
x_name <- x
|
||||
ggplot2::geom_text(
|
||||
mapping = utils::modifyList(ggplot2::aes(), list(label = str2lang("lbl"), x = str2lang(x), y = str2lang("value"))),
|
||||
mapping = utils::modifyList(ggplot2::aes(), list(label = str2lang("lbl"), x = str2lang(x), y = str2lang("value"), group = str2lang(fill))),
|
||||
position = position,
|
||||
inherit.aes = FALSE,
|
||||
size = datalabels.size,
|
||||
colour = datalabels.colour,
|
||||
lineheight = 0.75,
|
||||
vjust = vjust,
|
||||
data = function(x) {
|
||||
transformed <- sir_df(
|
||||
data = x,
|
||||
|
||||
Reference in new issue
Block a user