Skip to content

Commit

Permalink
cleanups to autoplot
Browse files Browse the repository at this point in the history
  • Loading branch information
tanho63 committed Aug 25, 2023
1 parent dd90ed0 commit 0d9757a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
40 changes: 24 additions & 16 deletions R/9_autoplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...),
Expand All @@ -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)

Expand All @@ -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"
Expand All @@ -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))))
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down
42 changes: 24 additions & 18 deletions R/9_autoplot_week.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...),
Expand All @@ -39,23 +36,29 @@ 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)
h2h_winpct <- NULL
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)

Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0d9757a

Please sign in to comment.