# \donttest{
library(mlbplotR)
library(ggplot2)
df <- data.frame(
a = c(rep(1:3, 3), 1.5, 2.5),
b = c(sort(rep(1:3, 3), decreasing = TRUE), 2.5, 2.5),
player_id = c("665742",
"677594",
"592450",
"608070",
"694973",
"518692",
"0",
"663728",
"120074",
"608369",
"606466"),
player_name = c("Juan Soto",
"Julio Rodríguez",
"Aaron Judge",
"José Ramírez",
"Paul Skenes",
"Freddie Freeman",
"Non Match",
"Cal Raleigh",
"David Ortiz",
"Corey Seager",
"Ketel Marte")
)
# set a custom fill colour for one player
df$colour <- ifelse(df$a == 2 & df$b == 2, NA, "b/w")
# scatterplot of the headshots
ggplot(df, aes(x = a, y = b)) +
geom_mlb_headshots(aes(player_id = player_id), height = 0.2) +
geom_label(aes(label = player_name), nudge_y = -0.35, alpha = 0.5) +
coord_cartesian(xlim = c(0.75, 3.25), ylim = c(0.7, 3.25)) +
theme_void()
# apply alpha as constant
ggplot(df, aes(x = a, y = b)) +
geom_mlb_headshots(aes(player_id = player_id), height = 0.2, alpha = 0.5) +
geom_label(aes(label = player_name), nudge_y = -0.35, alpha = 0.5) +
coord_cartesian(xlim = c(0.75, 3.25), ylim = c(0.7, 3.25)) +
theme_void()
# apply colour as an aesthetic and use the dot version with colored background
ggplot(df, aes(x = a, y = b)) +
geom_mlb_dot_headshots(aes(player_id = player_id, colour = colour), height = 0.2) +
geom_label(aes(label = player_name), nudge_y = -0.35, alpha = 0.5) +
coord_cartesian(xlim = c(0.75, 3.25), ylim = c(0.7, 3.25)) +
scale_colour_identity() +
theme_void()
# }
Run the code above in your browser using DataLab