Skip to content

Instantly share code, notes, and snippets.

@Tadge-Analytics
Created February 19, 2020 23:57
Show Gist options
  • Save Tadge-Analytics/8fbd26176fc1c998679620f02a3a7ebc to your computer and use it in GitHub Desktop.
Save Tadge-Analytics/8fbd26176fc1c998679620f02a3a7ebc to your computer and use it in GitHub Desktop.
library(lubridate)
library(tidyverse)
library(rlang)
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()))
function_arguments <- c("dataset" = "new_table",
"date_col" = "Date",
"sales_col" = "total_sales")
new_yearly_lines_fn <- function(dataset, date_col, sales_col) {
if (class(dataset) == "character") {
dataset <- eval(sym(dataset)) }
if (class(date_col) == "character") {
date_col <- sym(date_col) }
else
{date_col <- sym(quote(date_col))}
if (class(sales_col) == "character") {
sales_col <- sym(sales_col) }
else
{sales_col <- sym(quote(sales_col))}
dataset %>%
group_by(year_month = floor_date({{date_col}}, "months"),
year = year({{date_col}})) %>%
summarise(total_sales = sum({{sales_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")
}
new_yearly_lines_fn(new_table, Date, total_sales)
new_yearly_lines_fn(function_arguments[["dataset"]],
function_arguments[["date_col"]],
function_arguments[["sales_col"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment