#' Generate a simple plotting function of the utility for a given price
#'
#' 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 cost by default ==0 a constant to reduce from utility
#' @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 Expected Utility
#'
#'
#' @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_utility_of_pay(x=input,jump=5)
#'
#' @importFrom magrittr %>%
#' @importFrom dplyr pull
#' @importFrom dplyr select
#' @importFrom dplyr ends_with
#' @importFrom dplyr mutate
#' @importFrom dplyr arrange
#' @importFrom dplyr desc
#' @importFrom dplyr slice
#'
#' @importFrom dplyr filter
#' @importFrom ggplot2 ggplot
#' @importFrom ggplot2 aes
#' @importFrom ggplot2 geom_point
#' @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
#' @importFrom ggplot2 geom_smooth
#'
#' @export
gg_utility_of_pay <- function(x,
jump=5,
float=0.5,
cost=0,
tit="",
xsize=15,
yl="Expected Utility (Price * Willingness to Pay)",
xl="Price (ILS)"){
x_min <- min(x$price)
x_max <- max(x$price)
x <- x %>% mutate(utility=utility-cost)
x <- ggplot(x, aes(x = price, y = utility)) +
geom_smooth(method = 'loess',formula = "y ~ x") +
# geom_label(data = x %>%
# slice(seq(x_min, x_max, 50)),
# aes(label = paste0(round(utility), ", ", round(willing*100), "%")),
# position = position_nudge(y=position))+
geom_label(data = x %>%
slice(seq(x_min, x_max, jump)),
aes(label = round(utility)),
position = position_nudge(y=float))+
geom_point(data = x %>%
slice(seq(x_min, x_max, jump)),color="red")+
scale_x_continuous(breaks = seq(x_min,x_max,jump))+
ylab(yl)+
xlab(xl)+
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=15),
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.