2020-11-11 01:02:22 +03:00
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License. See LICENSE.txt in the project root for license information.
|
|
|
|
# --------------------------------------------------------------------------------------------
|
|
|
|
|
2020-11-19 15:41:01 +03:00
|
|
|
#' @title Create a line chart without aggregation for any metric
|
2020-11-07 00:36:17 +03:00
|
|
|
#'
|
|
|
|
#' @description
|
|
|
|
#' This function creates a line chart directly from the aggregated / summarised data.
|
|
|
|
#' Unlike `create_line()` which performs a person-level aggregation, there is no
|
|
|
|
#' calculation for `create_line_asis()` and the values are rendered as they are passed
|
|
|
|
#' into the function. The only requirement is that a `date_var` is provided for the x-axis.
|
|
|
|
#'
|
|
|
|
#' @param data Plotting data as a data frame.
|
|
|
|
#' @param date_var String containing name of variable for the horizontal axis.
|
|
|
|
#' @param metric String containing name of variable representing the line.
|
|
|
|
#' @param title Title of the plot.
|
|
|
|
#' @param subtitle Subtitle of the plot.
|
|
|
|
#' @param caption Caption of the plot.
|
|
|
|
#' @param ylab Y-axis label for the plot (group axis)
|
|
|
|
#' @param xlab X-axis label of the plot (bar axis).
|
|
|
|
#' @param line_colour String to specify colour to use for the line.
|
|
|
|
#' Hex codes are accepted. You can also supply
|
|
|
|
#' RGB values via `rgb2hex()`.
|
|
|
|
#'
|
|
|
|
#' @import ggplot2
|
|
|
|
#' @import dplyr
|
|
|
|
#'
|
2021-03-04 21:33:28 +03:00
|
|
|
#' @family Visualization
|
2020-11-19 15:41:01 +03:00
|
|
|
#' @family Flexible
|
2021-03-04 21:33:28 +03:00
|
|
|
#' @family Time-series
|
|
|
|
#'
|
2021-03-31 13:12:39 +03:00
|
|
|
#' @return
|
|
|
|
#' Returns a 'ggplot' object representing a line plot.
|
|
|
|
#'
|
2021-03-04 21:33:28 +03:00
|
|
|
#' @examples
|
|
|
|
#' library(dplyr)
|
|
|
|
#'
|
|
|
|
#' # Median `Emails_sent` grouped by `Date`
|
|
|
|
#' # Without Person Averaging
|
|
|
|
#' med_df <-
|
|
|
|
#' sq_data %>%
|
|
|
|
#' group_by(Date) %>%
|
|
|
|
#' summarise(Emails_sent_median = median(Emails_sent))
|
|
|
|
#'
|
|
|
|
#' med_df %>%
|
|
|
|
#' create_line_asis(
|
|
|
|
#' date_var = "Date",
|
|
|
|
#' metric = "Emails_sent_median",
|
|
|
|
#' title = "Median Emails Sent",
|
|
|
|
#' subtitle = "Person Averaging Not Applied",
|
|
|
|
#' caption = extract_date_range(sq_data, return = "text")
|
|
|
|
#' )
|
2020-11-07 00:36:17 +03:00
|
|
|
#'
|
|
|
|
#' @export
|
|
|
|
create_line_asis <- function(data,
|
|
|
|
date_var = "Date",
|
|
|
|
metric,
|
|
|
|
title = NULL,
|
|
|
|
subtitle = NULL,
|
|
|
|
caption = NULL,
|
|
|
|
ylab = date_var,
|
|
|
|
xlab = metric,
|
|
|
|
line_colour = rgb2hex(0, 120, 212)){
|
|
|
|
|
|
|
|
returnPlot <-
|
|
|
|
data %>%
|
|
|
|
mutate_at(vars(date_var), ~as.Date(., format = "%m/%d/%Y")) %>%
|
|
|
|
ggplot(aes(x = !!sym(date_var), y = !!sym(metric))) +
|
|
|
|
geom_line(colour = line_colour)
|
|
|
|
|
|
|
|
returnPlot +
|
|
|
|
labs(title = title,
|
|
|
|
subtitle = subtitle,
|
|
|
|
caption = caption,
|
|
|
|
y = xlab,
|
|
|
|
x = ylab) +
|
|
|
|
theme_wpa_basic()
|
|
|
|
}
|