Skip to content

Instantly share code, notes, and snippets.

@Tadge-Analytics
Created February 19, 2020 07:28
Show Gist options
  • Save Tadge-Analytics/c18345cb3cc31342c3f371c945e65708 to your computer and use it in GitHub Desktop.
Save Tadge-Analytics/c18345cb3cc31342c3f371c945e65708 to your computer and use it in GitHub Desktop.
library(rlang)
library()
set.seed(123)
new_table <- tibble(Date = seq.Date(as.Date("2016-01-01"), as.Date("2019-12-31"), 1)) %>%
mutate(total_sales = rnorm(n()))
yearly_lines_fn <- function(sales_table, date_col, money_col) {
date_col <- enquo(date_col)
money_col <- enquo(money_col)
sales_table %>%
group_by(year_month = floor_date(!! date_col, "months"),
year = year(!! date_col)) %>%
summarise(total_sales = sum(!! money_col)) %>%
ungroup() %>%
ggplot() +
aes(year_month, total_sales, col = factor(year)) +
geom_line(stat = "identity", size = 2) +
geom_point(stat = "identity", size = 2, col = "black")
}
yearly_lines_fn(new_table, Date, total_sales)
new_yearly_lines_fn <- function(sales_table, date_col, money_col) {
sales_table <- eval(sym(sales_table))
sales_table %>%
group_by(year_month = floor_date({{date_col}}, "months"),
year = year({{date_col}})) %>%
summarise(total_sales = sum({{money_col}})) %>%
ungroup() %>%
ggplot() +
aes(year_month, total_sales, col = factor(year)) +
geom_line(stat = "identity", size = 2) +
geom_point(stat = "identity", size = 2, col = "black")
}
function_arguments <- c("the_dataset" = "new_table",
"the_date_col" = "Date",
"the_money_col" = "total_sales")
new_yearly_lines_fn(function_arguments[["the_dataset"]], function_arguments[["the_date_col"]], function_arguments[["the_money_col"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment