R/gg_willingness_to_pay.R

Defines functions gg_willingness_to_pay

Documented in gg_willingness_to_pay

#' Generate a simple plotting function of the willingness to buy
#'
#' Function expects "make_willingness_utility" function output as data
#'
#'
#' @param x a tbl() that is the output of make_willingness_utility()
#' @param jump by default ==5  jumps between labels
#' @param tit by default = "Willingness to Pay",
#' @param xsize by default = 15, text size
#' @param yl y label, by default = "Percent Willing to Pay"
#' @param xl x label, by default = "Price (ILS)"
#'
#'
#' @return the function generates ggplot curve of the willingness to buy
#'
#'
#' @examples
#'  quality_assurance <- data.frame(
#'  ID = 1:500,
#'  money_willing_to_pay = sample(seq(0,20,5), size = 500, replace = T),
#'  year = sample(c("2018", "2019", "2020"), size = 500, replace = T))
#'
#'  input <- quality_assurance %>%
#'    saridr::make_willingness_utility(money_pay_var_string = "money_willing_to_pay")
#'
#'  gg_willingness_to_pay(x=input,jump=5)
#'
#' @importFrom magrittr %>%
#' @importFrom dplyr sym
#' @importFrom dplyr pull
#' @importFrom dplyr select
#' @importFrom dplyr ends_with
#' @importFrom dplyr group_by
#' @importFrom dplyr ungroup
#' @importFrom dplyr mutate
#' @importFrom dplyr arrange
#' @importFrom dplyr desc
#' @importFrom dplyr slice
#' @importFrom dplyr filter
#'
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_line
#' @importFrom ggplot2 scale_y_continuous
#' @importFrom ggplot2 scale_x_continuous
#' @importFrom ggplot2 geom_label
#' @importFrom ggplot2 guides
#' @importFrom ggplot2 guide_legend
#' @importFrom ggplot2 theme_bw
#' @importFrom ggplot2 xlab
#' @importFrom ggplot2 ylab
#' @importFrom ggplot2 theme
#' @importFrom ggplot2 ggtitle
#'
#' @export



gg_willingness_to_pay <- function(x,
                                    jump=5,
                                 tit="Willingness to Pay",
                                 xsize=15,
                                 yl="Percent Willing to Pay",
                                 xl="Price (ILS)"){

  x_min <- min(x$price)
  x_max <- max(x$price)

  x <- ggplot(x, aes(x = price, y = prop_willing)) +
    geom_line() +
    geom_label(data = x %>%
                 slice(seq(x_min, x_max, jump)),
               aes(label = paste0(round(prop_willing*100), "%")))+
    ylab(yl)+
    xlab(xl)+
    scale_y_continuous(labels=scales::percent_format(accuracy = 1))+
    scale_x_continuous(breaks = seq(x_min,x_max,jump))+
    theme(axis.text.x = element_text(size=xsize),
          # axis.title.x = element_blank(),
          axis.ticks.x = element_blank(),
          legend.position = "none",legend.box = "horizontal",
          legend.title = element_blank(),
          legend.text = element_text(size=xsize),
          plot.title = element_text(hjust = 0.5, vjust=2.12),
          panel.background = element_rect(fill = "transparent"),
          plot.background = element_rect(fill = "transparent", color = NA))+
    ggtitle(tit)

  return(x)

}
sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.