View source: R/mlb_tier_maker.R
mlb_player_tiers | R Documentation |
This function sets up a ggplot to visualize MLB player tiers.
Adapted from nflplotR
mlb_player_tiers(
data,
title = "MLB Player Tiers",
subtitle = "Created with the #mlbplotR Tiermaker",
caption = NULL,
tier_desc = c(`1` = "MVP Candidates", `2` = "Very Good", `3` = "Medium", `4` = "Bad",
`5` = "Negative WAR", `6` = "", `7` = ""),
presort = FALSE,
alpha = 1,
width = 0.1,
no_line_below_tier = NULL,
devel = FALSE,
background_color = "#1e1e1e",
line_color = "#e0e0e0",
title_color = "white",
subtitle_color = "#8e8e93",
caption_color = subtitle_color,
tier_label_color = title_color,
headshot_type = "main",
na_headshot_to_logo = TRUE
)
data |
A data frame that has to include the variables |
title |
The title of the plot. If |
subtitle |
The subtitle of the plot. If |
caption |
The caption of the plot. If |
tier_desc |
A named vector consisting of the tier descriptions. The names
must equal the tier numbers from |
presort |
If |
alpha |
The alpha channel of the logos, i.e. transparency level, as a numerical value between 0 and 1. Defaults to 1 |
width |
The desired width of the logo in |
no_line_below_tier |
Vector of tier numbers. The function won't draw tier separation lines below these tiers. This is intended to be used for tiers that shall be combined (see examples). |
devel |
Determines if headshots shall be rendered. If |
background_color |
Background color for the plot. Defaults to "#1e1e1e" |
line_color |
Line color for the plot. Defaults to "#e0e0e0" |
title_color |
Text color for the title. Defaults to "white" |
subtitle_color |
Text color the the subtitle. Defaults to "#8e8e93" |
caption_color |
Text color the the caption. Defaults to be equal to the subtitle |
tier_label_color |
Text color for the tier labels. Defaults to be equal to the title |
headshot_type |
"main" or "dot" headshots? Defaults to "main" |
na_headshot_to_logo |
Should NA/non-matches return the MLB logo instead of a grayed out blank headshot? Defaults to |
A plot object created with ggplot2::ggplot()
.
library(ggplot2)
library(dplyr, warn.conflicts = FALSE)
player_ids <- load_headshots() |>
head(35) |>
pull(savant_id)
# Build the player tiers data frame
# This is completely random!
df <- data.frame(
tier_no = sample(1:5, length(player_ids), replace = TRUE),
player_id = player_ids
) %>%
dplyr::group_by(tier_no) %>%
dplyr::mutate(tier_rank = sample(1:n(), n()))
# Plot player tiers
mlb_player_tiers(df)
# Create a combined tier which is useful for tiers with lots of players that
# should be split up in two or more rows. This is done by setting an empty
# string for the tier 5 description and removing the tier separation line
# below tier number 4.
# This example also shows how to turn off the subtitle and add a caption
mlb_player_tiers(df,
subtitle = NULL,
caption = "This is the caption",
tier_desc = c("1" = "MVP Candidates",
"2" = "Very Good",
"3" = "Medium",
"4" = "A Combined Tier",
"5" = ""),
no_line_below_tier = 4)
# For the development of the tiers, it can be useful to turn off image
# rendering as this can take quite a long time. By setting `devel = TRUE`, the
# headshots are replaced by player ids which is much faster
mlb_player_tiers(df,
tier_desc = c("1" = "MVP Candidates",
"2" = "Very Good",
"3" = "",
"4" = "A Combined Tier",
"5" = ""),
no_line_below_tier = c(2, 4),
devel = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.