R/demographic_plots.R

Defines functions plot_age_sex plot_treatment_group plot_race plot_sex

Documented in plot_age_sex plot_race plot_sex plot_treatment_group

#' Create the Number of Subjects by Sex Plot
#'
#' @param data a data frame (tibble) containing patient data
#'
#' @return a ggplot object
#' @export
#'
#' @examples
#' \dontrun{
#' plot_sex(patient_data)
#' }
plot_sex <- function(data) {
    if (!tibble::is_tibble(data)) {
        rlang::abort("This function expects data as tibble.")
    }

    data %>%
        dplyr::count(.data$sex) %>%
        ggplot2::ggplot(ggplot2::aes(x = .data$sex, y = .data$n)) +
        ggplot2::geom_col() +
        ggplot2::geom_text(ggplot2::aes(label = scales::comma(.data$n)),
                           hjust = 0.5,
                           nudge_y = 12) +
        hrbrthemes::theme_ipsum(grid = "Y") +
        ggplot2::labs(y = "Count",
                      x = "Sex",
                      title = "Number of subjects by sex")
}

#' Create the Number of Subjects by Race Plot
#'
#' @inheritParams plot_sex
#'
#' @return a ggplot object
#' @export
#'
#' @examples
#' \dontrun{
#' plot_race(patient_data)
#' }
plot_race <- function(data) {

    if (!tibble::is_tibble(data)) {
        rlang::abort("This function expects data as tibble.")
    }

    data %>%
        dplyr::count(.data$race) %>%
        dplyr::arrange(.data$n) %>%
        dplyr::mutate(race = factor(.data$race, levels = .data$race)) %>%
        ggplot2::ggplot(ggplot2::aes(.data$race, .data$n)) +
        ggplot2::geom_col() +
        ggplot2::coord_flip() +
        hrbrthemes::theme_ipsum(grid = "X") +
        ggplot2::labs(x = "Race",
                      y = "Count",
                      title = "Number of subjects by race")
}

#' Create the Number of Subjects by Treatment Group Plot
#'
#' @inheritParams plot_sex
#'
#' @return a ggplot object
#' @export
#'
#' @examples
#' \dontrun{
#' plot_treatment_group(patient_data)
#' }
plot_treatment_group <- function(data) {

    if (!tibble::is_tibble(data)) {
        rlang::abort("This function expects data as tibble.")
    }

    data %>%
        dplyr::count(.data$actarm) %>%
        ggplot2::ggplot(ggplot2::aes(.data$actarm, .data$n)) +
        ggplot2::geom_col() +
        ggplot2::labs(x = "Treatment",
                      y = "Count",
                      title = "Number of subjects by treatment group") +
        hrbrthemes::theme_ipsum(grid = "Y")
}

#' Create the Age Distribution by Sex Plot
#'
#' @inheritParams plot_sex
#'
#' @return a ggplot object
#' @export
#'
#' @examples
#' \dontrun{
#' plot_age_sex(patient_data)
#' }
plot_age_sex <- function(data) {

    if (!tibble::is_tibble(data)) {
        rlang::abort("This function expects data as tibble.")
    }

    ggplot2::ggplot(data, ggplot2::aes(.data$sex, .data$age)) +
        ggplot2::geom_boxplot() +
        ggplot2::labs(title = "Age distribution by Sex",
                      y = "Age (years)",
                      x = "Sex") +
        hrbrthemes::theme_ipsum(grid = "Y")
}
dragosmg/rocheshiny documentation built on April 12, 2020, 12:22 a.m.