From 0d9757ab679bdbe503765569ca7a85157237efc1 Mon Sep 17 00:00:00 2001 From: Tan Ho Date: Thu, 24 Aug 2023 21:27:23 -0400 Subject: [PATCH] cleanups to autoplot --- DESCRIPTION | 4 ++-- R/9_autoplot.R | 40 ++++++++++++++++++++++++---------------- R/9_autoplot_week.R | 42 ++++++++++++++++++++++++------------------ 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 307825f..bc7eefe 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,8 +29,8 @@ Imports: tidytable (>= 0.6.4) Suggests: covr (>= 3.0.0), - ggplot2 (>= 3.0.0), - ggridges (>= 0.5.0), + ggplot2 (>= 3.4.0), + ggridges (>= 0.5.4), knitr (>= 1.0), progressr (>= 0.8.0), rmarkdown (>= 2.6), diff --git a/R/9_autoplot.R b/R/9_autoplot.R index 1ab9f10..6b423cd 100644 --- a/R/9_autoplot.R +++ b/R/9_autoplot.R @@ -28,10 +28,7 @@ autoplot.ff_simulation <- function(object, ...) { type <- rlang::arg_match(type) - if (!requireNamespace("ggplot2", quietly = TRUE) && - !requireNamespace("ggridges", quietly = TRUE)) { - stop("`ggplot2` and `ggridges` must be installed to use `autoplot`.", call. = FALSE) - } + rlang::check_installed(c("ggplot2 (>= 3.4.0)", "ggridges")) switch(type, "wins" = p <- .ffs_plot_wins(object, ...), @@ -49,8 +46,12 @@ autoplot.ff_simulation <- function(object, h2h_wins <- NULL franchise_name <- NULL - ss_levels <- ss[,.(franchise_name,h2h_wins = stats::median(h2h_wins, na.rm = TRUE)),by = "franchise_name" - ][order(h2h_wins)] + ss_levels <- ss[ + , list(franchise_name,h2h_wins = stats::median(h2h_wins, na.rm = TRUE)) + ,by = "franchise_name" + ][ + order(h2h_wins) + ] ss$franchise_name <- factor(ss$franchise_name, levels = ss_levels$franchise_name) @@ -77,7 +78,6 @@ autoplot.ff_simulation <- function(object, ggplot2::ylab(NULL) + ggplot2::theme_minimal() + ggplot2::theme( - # legend.position = "none", panel.grid.major.y = ggplot2::element_blank(), panel.grid.minor.x = ggplot2::element_blank(), plot.title.position = "plot" @@ -98,11 +98,17 @@ autoplot.ff_simulation <- function(object, franchise_name <- NULL season_rank <- NULL - ss[,`:=`(season_rank = rank(-h2h_wins, ties.method = "min")), by = "season"] + ss[ + ,`:=`(season_rank = rank(-h2h_wins, ties.method = "min")) + , by = "season" + ] + ss_levels <- ss[ - ,.(franchise_name,season_rank = mean(season_rank, ties.method = "min")), - by = "franchise_name" - ][order(season_rank)] + , list(franchise_name, season_rank = mean(season_rank, ties.method = "min")) + , by = "franchise_name" + ][ + order(season_rank) + ] ss$franchise_name <- factor(ss$franchise_name, levels = ss_levels$franchise_name) ss$rank_label <- factor(scales::ordinal(ss$season_rank), scales::ordinal(sort(unique(ss$season_rank)))) @@ -136,7 +142,12 @@ autoplot.ff_simulation <- function(object, sw <- object$summary_week data.table::setDT(sw) team_score <- NULL - sw_levels <- sw[,.(team_score = stats::median(team_score, na.rm = TRUE)),by = c("franchise_name")][order(team_score)] + sw_levels <- sw[ + , list(team_score = stats::median(team_score, na.rm = TRUE)) + ,by = c("franchise_name") + ][ + order(team_score) + ] sw$franchise_name <- factor(sw$franchise_name, levels = sw_levels$franchise_name) ggplot2::ggplot( @@ -175,10 +186,7 @@ autoplot.ff_simulation <- function(object, #' @param y Ignored, required for compatibility with the `plot()` generic. #' @export plot.ff_simulation <- function(x, ..., type = c("wins", "rank", "points"), y) { - if (!requireNamespace("ggplot2", quietly = TRUE) && - !requireNamespace("ggridges", quietly = TRUE)) { - stop("`ggplot2` and `ggridges` must be installed to use `plot`.", call. = FALSE) - } + rlang::check_installed(c("ggplot2 (>= 3.4.0)", "ggridges")) type <- rlang::arg_match(type) diff --git a/R/9_autoplot_week.R b/R/9_autoplot_week.R index ca2dfe4..15b210f 100644 --- a/R/9_autoplot_week.R +++ b/R/9_autoplot_week.R @@ -27,10 +27,7 @@ autoplot.ff_simulation_week <- function(object, ...) { type <- rlang::arg_match(type) - if (!requireNamespace("ggplot2", quietly = TRUE) && - !requireNamespace("ggridges", quietly = TRUE)) { - stop("`ggplot2` and `ggridges` must be installed to use `autoplot`.", call. = FALSE) - } + rlang::check_installed(c("ggplot2 (>= 3.4.0)", "ggridges")) switch(type, "luck" = p <- .ffs_plot_week_luck(object, ...), @@ -39,9 +36,11 @@ autoplot.ff_simulation_week <- function(object, p } -.ffs_plot_week_luck <- function(object, ...) { +.ffs_plot_week_luck <- function(object, ..., caller_env = rlang::caller_env()) { - if(!object$simulation_params$actual_schedule) stop("Schedule luck plot not available if `actual_schedule` is FALSE") + if(!object$simulation_params$actual_schedule) { + cli::cli_abort("Schedule luck plot not available if `actual_schedule` is FALSE", call = caller_env) + } luck <- object$summary_simulation data.table::setDT(luck) @@ -49,13 +48,17 @@ autoplot.ff_simulation_week <- function(object, allplay_winpct <- NULL luck_pct <- NULL - luck <- luck[,`:=`(luck_pct = h2h_winpct - allplay_winpct) - ][,`:=`( - luck_label = scales::percent(luck_pct, accuracy = 0.1), - ap_hjust = ifelse(luck_pct > 0, 1.1, -0.1), - h2h_hjust = ifelse(luck_pct > 0, -0.1, 1.1) + luck <- luck[ + ,`:=`(luck_pct = h2h_winpct - allplay_winpct) + ][ + ,`:=`( + luck_label = scales::percent(luck_pct, accuracy = 0.1), + ap_hjust = ifelse(luck_pct > 0, 1.1, -0.1), + h2h_hjust = ifelse(luck_pct > 0, -0.1, 1.1) ) - ][order(h2h_winpct)] + ][ + order(h2h_winpct) + ] luck$franchise_name <- factor(luck$franchise_name, levels = luck$franchise_name) @@ -66,7 +69,7 @@ autoplot.ff_simulation_week <- function(object, color = .data$franchise_name ) ) + - ggplot2::geom_point(ggplot2::aes(x=.data$allplay_winpct), alpha = 0.8, size = 2) + + ggplot2::geom_point(ggplot2::aes(x = .data$allplay_winpct), alpha = 0.8, size = 2) + ggplot2::geom_segment( ggplot2::aes( x = .data$allplay_winpct, @@ -129,7 +132,13 @@ autoplot.ff_simulation_week <- function(object, sw <- object$summary_week data.table::setDT(sw) team_score <- NULL - sw_levels <- sw[,.(team_score = stats::median(team_score, na.rm = TRUE)),by = c("franchise_name")][order(team_score)] + sw_levels <- sw[ + , list(team_score = stats::median(team_score, na.rm = TRUE)) + , by = c("franchise_name") + ][ + order(team_score) + ] + sw$franchise_name <- factor(sw$franchise_name, levels = sw_levels$franchise_name) ggplot2::ggplot( @@ -166,10 +175,7 @@ autoplot.ff_simulation_week <- function(object, #' @param y Ignored, required for compatibility with the `plot()` generic. #' @export plot.ff_simulation_week <- function(x, ..., type = c("luck", "points"), y) { - if (!requireNamespace("ggplot2", quietly = TRUE) && - !requireNamespace("ggridges", quietly = TRUE)) { - stop("`ggplot2` and `ggridges` must be installed to use `plot`.", call. = FALSE) - } + rlang::check_installed(c("ggplot2 (>= 3.4.0)", "ggridges")) type <- rlang::arg_match(type)