Spaces:
Running
Running
Upload visualization_functions.R
Browse files- R/visualization_functions.R +15 -6
R/visualization_functions.R
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
blank_percentile_plot <- function(title = NULL) {
|
| 3 |
p <- ggplot() +
|
|
@@ -100,7 +107,7 @@ hitter_percentiles <- function(df, batter, season = NULL, batter_ids = NULL, hit
|
|
| 100 |
slice(1) # Take first row if multiple seasons
|
| 101 |
|
| 102 |
season_summary_df <- hitter_stats_df %>%
|
| 103 |
-
{ if (!is.null(season)) dplyr::filter(., Season == season) else . } %>%
|
| 104 |
dplyr::filter(PA >= 50) %>%
|
| 105 |
dplyr::transmute(
|
| 106 |
xwOBA = xwOBA,
|
|
@@ -122,7 +129,7 @@ hitter_percentiles <- function(df, batter, season = NULL, batter_ids = NULL, hit
|
|
| 122 |
|
| 123 |
if (nrow(season_summary_df) == 0) {
|
| 124 |
season_domain_data <- df %>%
|
| 125 |
-
{ if (!is.null(season)) dplyr::filter(., Season == season) else . } %>%
|
| 126 |
dplyr::filter(!is.na(Batter))
|
| 127 |
|
| 128 |
season_summary_df <- season_domain_data %>%
|
|
@@ -285,6 +292,8 @@ hitter_percentiles <- function(df, batter, season = NULL, batter_ids = NULL, hit
|
|
| 285 |
fielding_percentiles <- function(defense, batter, season, batter_ids = NULL) {
|
| 286 |
defense_season <- defense %>%
|
| 287 |
{ if (!is.null(season)) dplyr::filter(., Season == season) else . }
|
|
|
|
|
|
|
| 288 |
|
| 289 |
batter_split <- filter_rows_by_selection(
|
| 290 |
defense_season,
|
|
@@ -331,7 +340,7 @@ fielding_percentiles <- function(defense, batter, season, batter_ids = NULL) {
|
|
| 331 |
|
| 332 |
if (is.na(v)) return(NA_real_)
|
| 333 |
|
| 334 |
-
vals <- if (m == "FRV")
|
| 335 |
if (is.null(vals) || all(is.na(vals))) return(NA_real_)
|
| 336 |
mean(vals <= v, na.rm = TRUE) * 100
|
| 337 |
}),
|
|
@@ -1487,7 +1496,7 @@ pitcher_percentiles <- function(df, pitcher, season = NULL, pitcher_ids = NULL,
|
|
| 1487 |
}
|
| 1488 |
|
| 1489 |
season_extension_df <- df %>%
|
| 1490 |
-
{ if (!is.null(season)) dplyr::filter(., Season == season) else . } %>%
|
| 1491 |
dplyr::filter(!is.na(Pitcher)) %>%
|
| 1492 |
dplyr::group_by(Pitcher) %>%
|
| 1493 |
dplyr::summarise(
|
|
@@ -1513,7 +1522,7 @@ pitcher_percentiles <- function(df, pitcher, season = NULL, pitcher_ids = NULL,
|
|
| 1513 |
slice(1) # Take first row if multiple seasons
|
| 1514 |
|
| 1515 |
season_summary_df_pitcher <- pitcher_stats_df %>%
|
| 1516 |
-
{ if (!is.null(season)) dplyr::filter(., Season == season) else . } %>%
|
| 1517 |
dplyr::filter(BF >= 50) %>%
|
| 1518 |
dplyr::transmute(
|
| 1519 |
Name = Name,
|
|
@@ -1537,7 +1546,7 @@ pitcher_percentiles <- function(df, pitcher, season = NULL, pitcher_ids = NULL,
|
|
| 1537 |
|
| 1538 |
if (nrow(season_summary_df_pitcher) == 0) {
|
| 1539 |
season_domain_data <- df %>%
|
| 1540 |
-
{ if (!is.null(season)) dplyr::filter(., Season == season) else . } %>%
|
| 1541 |
dplyr::filter(!is.na(Pitcher))
|
| 1542 |
|
| 1543 |
season_summary_df_pitcher <- season_domain_data %>%
|
|
|
|
| 1 |
+
FALL_POOL_MAP <- c("Fall 2026" = "Fall 2025")
|
| 2 |
+
|
| 3 |
+
pool_season <- function(season) {
|
| 4 |
+
if (is.null(season) || length(season) != 1L || is.na(season)) return(season)
|
| 5 |
+
if (season %in% names(FALL_POOL_MAP)) unname(FALL_POOL_MAP[[season]]) else season
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
|
| 9 |
blank_percentile_plot <- function(title = NULL) {
|
| 10 |
p <- ggplot() +
|
|
|
|
| 107 |
slice(1) # Take first row if multiple seasons
|
| 108 |
|
| 109 |
season_summary_df <- hitter_stats_df %>%
|
| 110 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . } %>%
|
| 111 |
dplyr::filter(PA >= 50) %>%
|
| 112 |
dplyr::transmute(
|
| 113 |
xwOBA = xwOBA,
|
|
|
|
| 129 |
|
| 130 |
if (nrow(season_summary_df) == 0) {
|
| 131 |
season_domain_data <- df %>%
|
| 132 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . } %>%
|
| 133 |
dplyr::filter(!is.na(Batter))
|
| 134 |
|
| 135 |
season_summary_df <- season_domain_data %>%
|
|
|
|
| 292 |
fielding_percentiles <- function(defense, batter, season, batter_ids = NULL) {
|
| 293 |
defense_season <- defense %>%
|
| 294 |
{ if (!is.null(season)) dplyr::filter(., Season == season) else . }
|
| 295 |
+
defense_pool <- defense %>%
|
| 296 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . }
|
| 297 |
|
| 298 |
batter_split <- filter_rows_by_selection(
|
| 299 |
defense_season,
|
|
|
|
| 340 |
|
| 341 |
if (is.na(v)) return(NA_real_)
|
| 342 |
|
| 343 |
+
vals <- if (m == "FRV") defense_pool$frv else defense_pool$OAA
|
| 344 |
if (is.null(vals) || all(is.na(vals))) return(NA_real_)
|
| 345 |
mean(vals <= v, na.rm = TRUE) * 100
|
| 346 |
}),
|
|
|
|
| 1496 |
}
|
| 1497 |
|
| 1498 |
season_extension_df <- df %>%
|
| 1499 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . } %>%
|
| 1500 |
dplyr::filter(!is.na(Pitcher)) %>%
|
| 1501 |
dplyr::group_by(Pitcher) %>%
|
| 1502 |
dplyr::summarise(
|
|
|
|
| 1522 |
slice(1) # Take first row if multiple seasons
|
| 1523 |
|
| 1524 |
season_summary_df_pitcher <- pitcher_stats_df %>%
|
| 1525 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . } %>%
|
| 1526 |
dplyr::filter(BF >= 50) %>%
|
| 1527 |
dplyr::transmute(
|
| 1528 |
Name = Name,
|
|
|
|
| 1546 |
|
| 1547 |
if (nrow(season_summary_df_pitcher) == 0) {
|
| 1548 |
season_domain_data <- df %>%
|
| 1549 |
+
{ if (!is.null(season)) dplyr::filter(., Season == pool_season(season)) else . } %>%
|
| 1550 |
dplyr::filter(!is.na(Pitcher))
|
| 1551 |
|
| 1552 |
season_summary_df_pitcher <- season_domain_data %>%
|