R/geom_swimlane_text.R

Defines functions geom_swimlane_text

Documented in geom_swimlane_text

#' Add text to the end of each swimlane bar
#'
#' Most often used to annotate each bar with reason for ending study
#' or, if applicable, active status.
#'
#' @param x Typically subject identifier
#' @param y Typically duration on study
#' @param label Annotation text
#' @param ... Other options passed to ggtext::geom_richtext
#'
#' @export
#'
#' @examples
#' library(ggplot2)
#'
#' patient_disposition %>%
#'   dplyr::mutate(
#'     reason_off_study = tidyr::replace_na(reason_off_study, "→")
#'   ) %>%
#'   order_swimlane(subject, weeks_on_study, cohort) %>%
#'   ggplot() +
#'   geom_swimlane(subject, weeks_on_study, cohort) +
#'   ggsci::scale_fill_jco() +
#'   geom_swimlane_text(subject, weeks_on_study, reason_off_study) +
#'   theme_swimlane(legend.position = c(.9, .1)) %+replace%
#'   theme(axis.title.x = element_blank())
geom_swimlane_text <- function(x, y, label, ...) {

  x <- rlang::enexpr(x)
  y <- rlang::enexpr(y)
  label <- rlang::enexpr(label)

  ggtext::geom_richtext(
    ggplot2::aes(
      x = .data[[x]], y = .data[[y]], label = .data[[label]],
      ...
    ),
    hjust = 0,
    size = 2,
    label.size = 0,
    na.rm = TRUE,
    show.legend = FALSE
  )

}
tgerke/ggswimlane documentation built on Aug. 8, 2022, 12:29 p.m.