R/plot_count.R

Defines functions plot_count_all plot_count

Documented in plot_count plot_count_all

#' @title Plot Frequencies and Percentages
#' @name plot_count
#' @description Make bar plot of frequencies and percentages for one or more categorical variables.
#'
#' @param df A tibble in long form
#' @param interactive If true, plots are passed to plotly and display tooltips.
#'
#' @importFrom rlang parse_expr !! :=
#' @importFrom forcats fct_reorder
#' @importFrom dplyr mutate
#' @importFrom ggplot2 ggplot aes geom_col geom_text position_dodge expand_limits xlab ylab coord_flip
#' @importFrom plotly ggplotly
#' @importFrom tidyquant theme_tq
#' @importFrom stringr str_replace_all str_to_title
#'
#' @examples
#' plot_count(mtcars, cyl)
#'
#' @export

plot_count<- function(df, interactive = FALSE, ...) {

  var_name<- rlang::parse_expr(names(df)[1]) #Need the name of the variable being counted

  p<- ggplot2::ggplot(df, ggplot2::aes(x = !! var_name, y = n,
                                       text = paste0("Category N: ", n, "<br>",
                                                     "Percentage: ", pct, "<br>",
                                                     "Total N: ", total_n, "<br>")
  )) +
    ggplot2::geom_col(color = "#2c3e50", fill = "#18BC9C") +
    ggplot2::geom_text(aes(label = pct), position = ggplot2::position_dodge(width = .9),
                       hjust = -0.2) +
    ggplot2::expand_limits(y = c(0, max(df$n) + ((max(df$n) / 100) * 20) )) + ggplot2::coord_flip() +
    tidyquant::theme_tq() +
    ggplot2::xlab(paste0(names(df)[1])) + ggplot2::ylab("Number of Cases") +
    xlab(stringr::str_to_title(stringr::str_replace_all(rlang::as_string(var_name), "_", " ")))

  if(interactive == FALSE) {

    p

  } else {

    plotly::ggplotly(p, tooltip = "text")

  }

}


#' @title Plot All Frequencies and Percentages
#' @name plot_count_all
#' @description Plot frequencies & pecentages in bar chart for all categorical variables.
#'
#' @param df A list of tibbles generated by get_counts_all()
#' @param interactive If true, plots are passed to plotly and display tooltips.
#' @param n_rows How many rows should tbe present in he faceted panel of plots?
#' @param margins Margins to be passed to plotly::subplot() if interactive = TRUE.
#'
#' @importFrom purrr map
#' @importFrom dplyr group_by mutate
#' @importFrom ggplot2 ggplot geom_col geom_text expand_limits position_dodge xlab ylab coord_flip
#' @importFrom plotly ggplotly subplot
#' @importFrom forcats fct_reorder
#' @importFrom patchwork wrap_plots
#' @importFrom tidyquant theme_tq
#' @importFrom stringr str_replace str_to_title
#'
#' @examples
#' calc_count_all(mtcars) %>% plot_count_all()
#'
#' @export

plot_count_all<- function(df, interactive = FALSE, n_rows = 4, margins = c(0.059, 0.02, 0.02, 0.02)) {

  plot_list<- df %>% purrr::map( ~ {

    p<- ggplot2::ggplot(.x, ggplot2::aes(x = category, y = n,
                                     text = paste0("Category: ", category, "<br>",
                                                   "N: ", n, "<br>",
                                                   "Total Cases: ", total_n, "<br>",
                                                   "Category %: ", pct, "<br>"))) +
      ggplot2::geom_col(color = "#2c3e50", fill = "#18BC9C") +
      ggplot2::geom_text(aes(label = pct), size = 3, position = ggplot2::position_dodge(width = .9),
                         hjust = -0.2) +
      ggplot2::expand_limits(y = c(0, max(.x$n) + ((max(.x$n) / 100) * 20) )) + ggplot2::coord_flip() +
      tidyquant::theme_tq() +
      ggplot2::xlab(stringr::str_to_title(stringr::str_replace_all(paste0(.x$variable[1]), "_", " "))) + ggplot2::ylab("Number of Cases")

    if(interactive == FALSE) {

      p

    } else {

      plotly::ggplotly(p, tooltip = "text")

    }

  })


  if(interactive == FALSE) {

    patchwork::wrap_plots(plot_list, nrow = n_rows)

  } else {

    plotly::subplot(plot_list, nrows = n_rows, titleY=T, margin = margins)

  }



}
PsychlytxTD/fht documentation built on July 4, 2020, 5:42 p.m.