From 503c33cfaa1e1fefce426e7de151efaf0d0bc285 Mon Sep 17 00:00:00 2001 From: Jeff Rothschild Date: Thu, 4 May 2023 21:09:22 +1200 Subject: [PATCH 1/2] added the option for geom_points --- R/plot-anomaly_diagnostics.R | 278 ++++++++++++++++++++--------------- 1 file changed, 157 insertions(+), 121 deletions(-) diff --git a/R/plot-anomaly_diagnostics.R b/R/plot-anomaly_diagnostics.R index a99034c1..1e319c0b 100644 --- a/R/plot-anomaly_diagnostics.R +++ b/R/plot-anomaly_diagnostics.R @@ -119,46 +119,52 @@ #' @name plot_anomaly_diagnostics #' @export plot_anomaly_diagnostics <- function( - .data, .date_var, .value, - - .facet_vars = NULL, - - .frequency = "auto", - .trend = "auto", - .alpha = 0.05, - .max_anomalies = 0.2, - .message = TRUE, - - .facet_ncol = 1, - .facet_nrow = 1, - .facet_scales = "free", - .facet_dir = "h", - .facet_collapse = FALSE, - .facet_collapse_sep = " ", - .facet_strip_remove = FALSE, - - .line_color = "#2c3e50", - .line_size = 0.5, - .line_type = 1, - .line_alpha = 1, - - .anom_color = "#e31a1c", - .anom_alpha = 1, - .anom_size = 1.5, - - .ribbon_fill = "grey20", - .ribbon_alpha = 0.20, - - .legend_show = TRUE, - - .title = "Anomaly Diagnostics", - .x_lab = "", - .y_lab = "", - .color_lab = "Anomaly", - - .interactive = TRUE, - .trelliscope = FALSE, - .trelliscope_params = list() + .data, .date_var, .value, + + .facet_vars = NULL, + + .frequency = "auto", + .trend = "auto", + .alpha = 0.05, + .max_anomalies = 0.2, + .message = TRUE, + + .facet_ncol = 1, + .facet_nrow = 1, + .facet_scales = "free", + .facet_dir = "h", + .facet_collapse = FALSE, + .facet_collapse_sep = " ", + .facet_strip_remove = FALSE, + + .line_color = "#2c3e50", + .line_size = 0.5, + .line_type = 1, + .line_alpha = 1, + + .point_shape = 16, + .point_size = 0.001, + .point_alpha = 1, + .point_color = "black", + .point_fill = NA, + + .anom_color = "#e31a1c", + .anom_alpha = 1, + .anom_size = 1.5, + + .ribbon_fill = "grey20", + .ribbon_alpha = 0.20, + + .legend_show = TRUE, + + .title = "Anomaly Diagnostics", + .x_lab = "", + .y_lab = "", + .color_lab = "Anomaly", + + .interactive = TRUE, + .trelliscope = FALSE, + .trelliscope_params = list() ) { # Checks @@ -181,46 +187,52 @@ plot_anomaly_diagnostics <- function( #' @export plot_anomaly_diagnostics.data.frame <- function( - .data, .date_var, .value, - - .facet_vars = NULL, - - .frequency = "auto", - .trend = "auto", - .alpha = 0.05, - .max_anomalies = 0.2, - .message = TRUE, - - .facet_ncol = 1, - .facet_nrow = 1, - .facet_scales = "free", - .facet_dir = "h", - .facet_collapse = FALSE, - .facet_collapse_sep = " ", - .facet_strip_remove = FALSE, - - .line_color = "#2c3e50", - .line_size = 0.5, - .line_type = 1, - .line_alpha = 1, - - .anom_color = "#e31a1c", - .anom_alpha = 1, - .anom_size = 1.5, - - .ribbon_fill = "grey20", - .ribbon_alpha = 0.20, - - .legend_show = TRUE, - - .title = "Anomaly Diagnostics", - .x_lab = "", - .y_lab = "", - .color_lab = "Anomaly", - - .interactive = TRUE, - .trelliscope = FALSE, - .trelliscope_params = list() + .data, .date_var, .value, + + .facet_vars = NULL, + + .frequency = "auto", + .trend = "auto", + .alpha = 0.05, + .max_anomalies = 0.2, + .message = TRUE, + + .facet_ncol = 1, + .facet_nrow = 1, + .facet_scales = "free", + .facet_dir = "h", + .facet_collapse = FALSE, + .facet_collapse_sep = " ", + .facet_strip_remove = FALSE, + + .line_color = "#2c3e50", + .line_size = 0.5, + .line_type = 1, + .line_alpha = 1, + + .point_shape = 16, + .point_size = 0.001, + .point_alpha = 1, + .point_color = "black", + .point_fill = NA, + + .anom_color = "#e31a1c", + .anom_alpha = 1, + .anom_size = 1.5, + + .ribbon_fill = "grey20", + .ribbon_alpha = 0.20, + + .legend_show = TRUE, + + .title = "Anomaly Diagnostics", + .x_lab = "", + .y_lab = "", + .color_lab = "Anomaly", + + .interactive = TRUE, + .trelliscope = FALSE, + .trelliscope_params = list() ) { # Tidy Eval Setup @@ -279,7 +291,7 @@ plot_anomaly_diagnostics.data.frame <- function( g <- data_formatted %>% ggplot2::ggplot(ggplot2::aes(!! date_var_expr, observed)) + ggplot2::labs(x = .x_lab, y = .y_lab, title = .title, color = .color_lab) + - theme_tq() + tidyquant::theme_tq() # Add facets if (length(facet_names) > 0) { @@ -307,6 +319,18 @@ plot_anomaly_diagnostics.data.frame <- function( alpha = .line_alpha ) + + # Add points + g <- g + + ggplot2::geom_point( + shape = .point_shape, + size = .point_size, + alpha = .point_alpha, + color = .point_color, + fill = .point_fill + ) + + # Add Outliers g <- g + ggplot2::geom_point(ggplot2::aes_string(color = "anomaly"), size = .anom_size, alpha = .anom_alpha, @@ -368,46 +392,52 @@ plot_anomaly_diagnostics.data.frame <- function( #' @export plot_anomaly_diagnostics.grouped_df <- function( - .data, .date_var, .value, - - .facet_vars = NULL, - - .frequency = "auto", - .trend = "auto", - .alpha = 0.05, - .max_anomalies = 0.2, - .message = TRUE, - - .facet_ncol = 1, - .facet_nrow = 1, - .facet_scales = "free", - .facet_dir = "h", - .facet_collapse = FALSE, - .facet_collapse_sep = " ", - .facet_strip_remove = FALSE, - - .line_color = "#2c3e50", - .line_size = 0.5, - .line_type = 1, - .line_alpha = 1, - - .anom_color = "#e31a1c", - .anom_alpha = 1, - .anom_size = 1.5, - - .ribbon_fill = "grey20", - .ribbon_alpha = 0.20, - - .legend_show = TRUE, - - .title = "Anomaly Diagnostics", - .x_lab = "", - .y_lab = "", - .color_lab = "Anomaly", - - .interactive = TRUE, - .trelliscope = FALSE, - .trelliscope_params = list() + .data, .date_var, .value, + + .facet_vars = NULL, + + .frequency = "auto", + .trend = "auto", + .alpha = 0.05, + .max_anomalies = 0.2, + .message = TRUE, + + .facet_ncol = 1, + .facet_nrow = 1, + .facet_scales = "free", + .facet_dir = "h", + .facet_collapse = FALSE, + .facet_collapse_sep = " ", + .facet_strip_remove = FALSE, + + .line_color = "#2c3e50", + .line_size = 0.5, + .line_type = 1, + .line_alpha = 1, + + .point_shape = 16, + .point_size = 0.001, + .point_alpha = 1, + .point_color = "black", + .point_fill = NA, + + .anom_color = "#e31a1c", + .anom_alpha = 1, + .anom_size = 1.5, + + .ribbon_fill = "grey20", + .ribbon_alpha = 0.20, + + .legend_show = TRUE, + + .title = "Anomaly Diagnostics", + .x_lab = "", + .y_lab = "", + .color_lab = "Anomaly", + + .interactive = TRUE, + .trelliscope = FALSE, + .trelliscope_params = list() ) { @@ -452,6 +482,12 @@ plot_anomaly_diagnostics.grouped_df <- function( .line_type = .line_type, .line_alpha = .line_alpha, + .point_shape = .point_shape, + .point_size = .point_size, + .point_alpha = .point_alpha, + .point_color = .point_color, + .point_fill = .point_fill, + .anom_color = .anom_color, .anom_alpha = .anom_alpha, .anom_size = .anom_size, From ebf8869de957cefe766ce1c69116984b14bf9596 Mon Sep 17 00:00:00 2001 From: Jeff Rothschild Date: Fri, 5 May 2023 06:16:06 +1200 Subject: [PATCH 2/2] Removed "tidyquant::" and added a T/F to show points, which meant the default point size could be 1 --- R/plot-anomaly_diagnostics.R | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/R/plot-anomaly_diagnostics.R b/R/plot-anomaly_diagnostics.R index 1e319c0b..e2c6c202 100644 --- a/R/plot-anomaly_diagnostics.R +++ b/R/plot-anomaly_diagnostics.R @@ -142,8 +142,9 @@ plot_anomaly_diagnostics <- function( .line_type = 1, .line_alpha = 1, + .show_points = FALSE, .point_shape = 16, - .point_size = 0.001, + .point_size = 1, .point_alpha = 1, .point_color = "black", .point_fill = NA, @@ -210,8 +211,9 @@ plot_anomaly_diagnostics.data.frame <- function( .line_type = 1, .line_alpha = 1, + .show_points = FALSE, .point_shape = 16, - .point_size = 0.001, + .point_size = 1, .point_alpha = 1, .point_color = "black", .point_fill = NA, @@ -291,7 +293,7 @@ plot_anomaly_diagnostics.data.frame <- function( g <- data_formatted %>% ggplot2::ggplot(ggplot2::aes(!! date_var_expr, observed)) + ggplot2::labs(x = .x_lab, y = .y_lab, title = .title, color = .color_lab) + - tidyquant::theme_tq() + theme_tq() # Add facets if (length(facet_names) > 0) { @@ -321,14 +323,18 @@ plot_anomaly_diagnostics.data.frame <- function( # Add points - g <- g + - ggplot2::geom_point( - shape = .point_shape, - size = .point_size, - alpha = .point_alpha, - color = .point_color, - fill = .point_fill - ) + if (.show_points == TRUE) { + + g <- g + + ggplot2::geom_point( + shape = .point_shape, + size = .point_size, + alpha = .point_alpha, + color = .point_color, + fill = .point_fill + ) + + } # Add Outliers @@ -415,8 +421,9 @@ plot_anomaly_diagnostics.grouped_df <- function( .line_type = 1, .line_alpha = 1, + .show_points = FALSE, .point_shape = 16, - .point_size = 0.001, + .point_size = 1, .point_alpha = 1, .point_color = "black", .point_fill = NA, @@ -482,6 +489,7 @@ plot_anomaly_diagnostics.grouped_df <- function( .line_type = .line_type, .line_alpha = .line_alpha, + .show_points = .show_points, .point_shape = .point_shape, .point_size = .point_size, .point_alpha = .point_alpha,