R/composition_barplot.R

Defines functions .composition_barplot

.composition_barplot <- function(x,
                                 x_cat,
                                 fill_cat,
                                 col_pal = fcexpr::col_pal("custom"),
                                 plot_labels = F) {


  if (!x_cat %in% names(x)) {
    stop("x_cat not found in x.")
  }
  if (!fill_cat %in% names(x)) {
    stop("fill_cat not found in x.")
  }

  table <-
    x %>%
    dplyr::count(!!rlang::sym(x_cat), !!rlang::sym(fill_cat)) %>%
    dplyr::left_join(dplyr::count(x, !!rlang::sym(x_cat), name = "total"), by = x_cat) %>%
    dplyr::mutate(rel = n/total)

  if (is.numeric(table[,fill_cat])) {
    table <- table %>% dplyr::mutate({{ fill_cat }} := as.character(!!rlang::sym(fill_cat)))
  }

  table <- tibble::as_tibble(table)

  plot <- ggplot2::ggplot(table, ggplot2::aes(x = !!rlang::sym(x_cat), y = rel, fill = !!rlang::sym(fill_cat))) +
    ggplot2::geom_col() +
    ggplot2::scale_fill_manual(values = col_pal)

  if (plot_labels) {
    plot <- plot + ggplot2::geom_text(ggplot2::aes(label = round(rel,2)), position = ggplot2::position_stack(vjust = 0.5))
  }

  return(list(table = table, plot = plot))
}
Close-your-eyes/fcexpr documentation built on Sept. 29, 2023, 12:27 a.m.