R/lunch_pie.R

Defines functions lunch_pie

Documented in lunch_pie

# Functions for plotting

#' Create Proportional Bar Chart
#' @description Creates a standardized bar chart for the data and variable combination.
#'
#' @param data The data frame or tibble
#' @param variable A string of the variable you wish to plot
#' @param title The title for the pie chart
#'
#' @return Returns a ggplot2 plot object.
#' @export
#' @import ggplot2
#' @import dplyr
#' @importFrom magrittr %>%
#' @importFrom scales pretty_breaks
#'
#' @examples
#' data_frame <- data.frame(Lunch_Eaten=c("Yes","No","Yes"),
#'                          Location=c("Desk", "Restaurant", "Desk"))
#' lunch_pie(data_frame, "Lunch_Eaten", "Percentage of Lunch Eaters")
lunch_pie <- function(data, variable, title) {
  variable_title = gsub(pattern = "_", replacement = " ", x = variable)
  data %>%
    group_by_(variable) %>%
    ggplot(aes(x=get(variable), fill=get(variable))) +
    geom_bar(stat="count") +
    scale_y_continuous("Frequency", breaks=pretty_breaks()) +
    labs(title=title, x="") +
    guides(fill=F) +
    theme_bw() +
    theme(
      axis.ticks = element_blank()
    )
}
jmiahjones/lunch.time documentation built on Dec. 14, 2017, 12:53 p.m.