R/helpers_plots.R

Defines functions heatmap_di violin_auc

#' @keywords internal
violin_auc <- function(auc_tbl,
                       summary_tbl = NULL,
                       progress_metric = c('audpc', 'audps'),
                       factors = 'trat') {

  progress_metric <- match.arg(progress_metric)

  required <- c(factors, 'AUC')
  missing_cols <- setdiff(required, names(auc_tbl))

  if (length(missing_cols) > 0L) {
    stop(
      'Missing required column(s): ',
      paste(missing_cols, collapse = ', '),
      call. = FALSE
    )
  }

  auc_tbl <- normalize_factors(
    auc_tbl,
    factors = factors
  )

  auc_tbl$.factor_group <- interaction(
    auc_tbl[factors],
    sep = ' \u00D7 ',
    drop = TRUE,
    lex.order = TRUE
  )

  x_label <- if (length(factors) == 1L && identical(factors, 'trat')) {
    'Treatment'
  } else {
    paste(factors, collapse = ' \u00D7 ')
  }

  g <- ggplot2::ggplot(
    auc_tbl,
    ggplot2::aes(
      x = .factor_group,
      y = AUC,
      fill = .factor_group
    )
  ) +
    ggplot2::geom_violin(trim = FALSE, alpha = 0.35) +
    ggplot2::geom_jitter(
      width = 0.08,
      alpha = 0.7,
      size = 1.6
    ) +
    ggplot2::theme_minimal() +
    ggplot2::labs(
      x = x_label,
      y = toupper(progress_metric)
    )

  if (!is.null(summary_tbl)) {

    required_summary <- c(factors, 'mean', 'lwr', 'upr')

    if (all(required_summary %in% names(summary_tbl))) {

      summary_tbl <- normalize_factors(
        summary_tbl,
        factors = factors
      )

      summary_tbl$.factor_group <- interaction(
        summary_tbl[factors],
        sep = ' \u00D7 ',
        drop = TRUE,
        lex.order = TRUE
      )

      g <- g +
        ggplot2::geom_point(
          data = summary_tbl,
          mapping = ggplot2::aes(
            x = .factor_group,
            y = mean
          ),
          shape = 21,
          size = 3,
          stroke = 0.8,
          fill = 'white',
          inherit.aes = FALSE
        ) +
        ggplot2::geom_errorbar(
          data = summary_tbl,
          mapping = ggplot2::aes(
            x = .factor_group,
            ymin = lwr,
            ymax = upr
          ),
          width = 0.2,
          linewidth = 0.7,
          inherit.aes = FALSE
        )
    }
  }

  g
}

heatmap_di <- function(di_tbl, factors = 'trat') {

  required <- c(factors, 'time', 'DI')
  missing_cols <- setdiff(required, names(di_tbl))

  if (length(missing_cols) > 0L) {
    stop(
      'Missing required column(s): ',
      paste(missing_cols, collapse = ', '),
      call. = FALSE
    )
  }

  di_tbl <- normalize_factors(
    di_tbl,
    factors = factors
  )

  agg <- di_tbl |>
    dplyr::group_by(
      dplyr::across(
        dplyr::all_of(c(factors, 'time'))
      )
    ) |>
    dplyr::summarize(
      DI = mean(DI, na.rm = TRUE),
      .groups = 'drop'
    )

  y_factor <- factors[1L]

  g <- ggplot2::ggplot(
    agg,
    ggplot2::aes(
      x = time,
      y = .data[[y_factor]],
      fill = DI
    )
  ) +
    ggplot2::geom_tile() +
    ggplot2::scale_x_continuous(
      expand = ggplot2::expansion(add = 0)
    ) +
    ggplot2::scale_y_discrete(
      expand = ggplot2::expansion(add = 0)
    ) +
    ggplot2::labs(
      x = 'Time',
      y = if (identical(factors, 'trat')) {
        'Treatment'
      } else {
        y_factor
      },
      fill = 'Mean DI'
    ) +
    ggplot2::theme_minimal()

  if (length(factors) > 1L) {
    facet_factors <- factors[-1L]

    facet_formula <- stats::as.formula(
      paste('~', paste(facet_factors, collapse = ' + '))
    )

    g <- g + ggplot2::facet_wrap(facet_formula)
  }

  g
}

Try the infectionIndex package in your browser

Any scripts or data that you put into this service are public.

infectionIndex documentation built on Dec. 4, 2026, 5:10 p.m.