英文:
How do you get and use data (logos) from R library (nflplotR) properly?
问题
I've been trying to figure out how to pull logos from nflplotR and display them in the visual but can't seem to figure out how to do it properly. I'm fairly new to R, so I think there's some nuances I am not good with.
library(nflfastR)
library(nflplotR)
library(tidyverse)
library(gghighcontrast)
library(scales)
library(ggimage)
library(ggthemes)
plot_for_data <-
function(data,
logos,
foreground_color,
background_color) {
# NOTE: Doesn't work well with facet_wrap()...need to specify
# team logos more dynamically based on the data being charted.
single_game_id <- data[1, ]$game_id
game_title_pieces <- strsplit(single_game_id, "_")[[1]]
game_year <- game_title_pieces[1]
game_week <- game_title_pieces[2]
# Get home_team and away_team and annotate on chart
home_team_abbr <- data[1, ]$home_team
away_team_abbr <- data[1, ]$away_team
# Build a data frame with coordinates of team logo to place on chart
logo_placement_data <- data.frame(
a = c(3600, 3600),
b = c(0.875, 0.125),
team_abbr = c(home_team_abbr, away_team_abbr),
stringsAsFactors = FALSE
) %>% inner_join(logos, by = "team_abbr)
#ggplot(logo_placement_data, aes(x = a, y = b)) +
# geom_nfl_logos(aes(team_abbr = c(home_team_abbr, away_team_abbr), alpha = alpha, colour = colour), width = 0.075) +
# geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
# scale_alpha_identity() +
# scale_color_identity() +
# theme_void()
plot <- ggplot(data,
aes(x = game_seconds_remaining, y = home_wp_custom)) +
# 50% reference line
geom_hline(yintercept = 0.5,
color = grey,
size = 1) +
# Reference line for each quarter (and halftime)
geom_vline(xintercept = 15 * 60, color = grey) +
geom_vline(xintercept = 30 * 60, color = grey) +
geom_vline(xintercept = 45 * 60, color = grey) +
annotate(
"text",
x = 58 * 60,
y = 0.95,
label = "Q1",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 43 * 60,
y = 0.95,
label = "Q2",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 28 * 60,
y = 0.95,
label = "Q3",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 13 * 60,
y = 0.95,
label = "Q4",
family = "InputMono",
color = grey,
size = 2
) +
# Win Probability
geom_line(aes(y = home_wp_post), color = grey) +
geom_line(size = 0.8) +
# Scoring events
geom_rug(
data = filter(data, away_scoring_play == 1),
color = foreground_color,
sides = "b",
size = 1.5
) +
geom_rug(
data = filter(data, home_scoring_play == 1),
color = foreground_color,
sides = "t",
size = 1.5
) +
# Draw home and away team logo
geom_image(
data = logo_placement_data,
aes(x = x, y = y, image = logos),
size = 0.08,
asp = 16 / 9
) +
# Formatting
scale_x_reverse() +
scale_y_continuous(labels = percent, limits = c(0, 1)) +
theme_high_contrast(
base_family = "InputMono",
background_color = background_color,
foreground_color = foreground_color
) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(
title = str_interp(
"${game_year} Week ${game_week}: ${away_team_abbr} at ${home_team_abbr}"
),
subtitle = "Custom win probability model compared to nflfastR WP (grey)",
caption = "Data from nflfastR",
x = "Quarters",
y = "Home Win Probability"
)
}
for (single_game_id in game_ids) {
plot <-
plot_for_data(
filter(pbp_data, game_id == single_game_id),
logos,
foreground_color,
background_color
)
ggsave(
str_interp("wp-${single_game_id}.png"),
plot = plot,
width = 6,
height = 4
)
}
英文:
I've been trying to figure out how to pull logos from nflplotR and display them in the visual but can't seem to figure out how to do it properly. I'm fairly new to R, so I think there's some nuances I am not good with.
I tried to remove the "logos" variable and tried a different approach as well but had no luck. It might be the way I'm pulling the data from the library but not really sure. Any help would be appreciated.
library(nflfastR)
library(nflplotR)
library(tidyverse)
library(gghighcontrast)
library(scales)
library(ggimage)
library(ggthemes)
plot_for_data <-
function(data,
logos,
foreground_color,
background_color) {
# NOTE: Doesn't work well with facet_wrap()...need to specify
# team logos more dynamically based on the data being charted.
single_game_id <- data[1, ]$game_id
game_title_pieces <- strsplit(single_game_id, "_")[[1]]
game_year <- game_title_pieces[1]
game_week <- game_title_pieces[2]
# Get home_team and away_team and annotate on chart
home_team_abbr <- data[1, ]$home_team
away_team_abbr <- data[1, ]$away_team
# Build a data frame with coordinates of team logo to place on chart
logo_placement_data <- data.frame(
a = c(3600, 3600),
b = c(0.875, 0.125),
team_abbr = c(home_team_abbr, away_team_abbr),
stringsAsFactors = FALSE
) %>% inner_join(logos, by = "team_abbr")
#ggplot(logo_placement_data, aes(x = a, y = b)) +
# geom_nfl_logos(aes(team_abbr = c(home_team_abbr, away_team_abbr), alpha = alpha, colour = colour), width = 0.075) +
# geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
# scale_alpha_identity() +
# scale_color_identity() +
# theme_void()
plot <- ggplot(data,
aes(x = game_seconds_remaining, y = home_wp_custom)) +
# 50% reference line
geom_hline(yintercept = 0.5,
color = grey,
size = 1) +
# Reference line for each quarter (and halftime)
geom_vline(xintercept = 15 * 60, color = grey) +
geom_vline(xintercept = 30 * 60, color = grey) +
geom_vline(xintercept = 45 * 60, color = grey) +
annotate(
"text",
x = 58 * 60,
y = 0.95,
label = "Q1",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 43 * 60,
y = 0.95,
label = "Q2",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 28 * 60,
y = 0.95,
label = "Q3",
family = "InputMono",
color = grey,
size = 2
) +
annotate(
"text",
x = 13 * 60,
y = 0.95,
label = "Q4",
family = "InputMono",
color = grey,
size = 2
) +
# Win Probability
geom_line(aes(y = home_wp_post), color = grey) +
geom_line(size = 0.8) +
# Scoring events
geom_rug(
data = filter(data, away_scoring_play == 1),
color = foreground_color,
sides = "b",
size = 1.5
) +
geom_rug(
data = filter(data, home_scoring_play == 1),
color = foreground_color,
sides = "t",
size = 1.5
) +
# Draw home and away team logo
geom_image(
data = logo_placement_data,
aes(x = x, y = y, image = logos),
size = 0.08,
asp = 16 / 9
) +
# Formatting
scale_x_reverse() +
scale_y_continuous(labels = percent, limits = c(0, 1)) +
theme_high_contrast(
base_family = "InputMono",
background_color = background_color,
foreground_color = foreground_color
) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(
title = str_interp(
"${game_year} Week ${game_week}: ${away_team_abbr} at ${home_team_abbr}"
),
subtitle = "Custom win probability model compared to nflfastR WP (grey)",
caption = "Data from nflfastR",
x = "Quarters",
y = "Home Win Probability"
)
}
for (single_game_id in game_ids) {
plot <-
plot_for_data(
filter(pbp_data, game_id == single_game_id),
logos,
foreground_color,
background_color
)
ggsave(
str_interp("wp-${single_game_id}.png"),
plot = plot,
width = 6,
height = 4
)
}
答案1
得分: 1
使用nflplotR::geom_nfl_logos
函数替代geom_image
。请查看此文档以了解如何向你的ggplot添加标志的不同示例。
根据你的代码,这应该可以工作:
ggplot(logo_placement_data, aes(x = a, y = b)) +
geom_nfl_logos(aes(team_abbr = team_abbr), width = 0.075) +
geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
scale_alpha_identity() +
scale_color_identity() +
theme_void()
英文:
Use the nflplotR::geom_nfl_logos
function instead of geom_image
. Check out this vignette for different examples of how to add logos to your ggplot.
Based on your code, this should work:
ggplot(logo_placement_data, aes(x = a, y = b)) +
geom_nfl_logos(aes(team_abbr = team_abbr), width = 0.075) +
geom_label(aes(label = teams), nudge_y = -0.35, alpha = 0.5) +
scale_alpha_identity() +
scale_color_identity() +
theme_void()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论