如何正确获取并使用R库(nflplotR)中的数据(标志)?

huangapple go评论63阅读模式
英文:

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 &lt;-
function(data,
logos,
foreground_color,
background_color) {
# NOTE: Doesn&#39;t work well with facet_wrap()...need to specify
# team logos more dynamically based on the data being charted.
single_game_id &lt;- data[1, ]$game_id
game_title_pieces &lt;- strsplit(single_game_id, &quot;_&quot;)[[1]]
game_year &lt;- game_title_pieces[1]
game_week &lt;- game_title_pieces[2]
# Get home_team and away_team and annotate on chart
home_team_abbr &lt;- data[1, ]$home_team
away_team_abbr &lt;- data[1, ]$away_team
# Build a data frame with coordinates of team logo to place on chart
logo_placement_data &lt;- data.frame(
a = c(3600, 3600),
b = c(0.875, 0.125),
team_abbr = c(home_team_abbr, away_team_abbr),
stringsAsFactors = FALSE
) %&gt;% inner_join(logos, by = &quot;team_abbr&quot;)
#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 &lt;- 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(
&quot;text&quot;,
x = 58 * 60,
y = 0.95,
label = &quot;Q1&quot;,
family = &quot;InputMono&quot;,
color = grey,
size = 2
) +
annotate(
&quot;text&quot;,
x = 43 * 60,
y = 0.95,
label = &quot;Q2&quot;,
family = &quot;InputMono&quot;,
color = grey,
size = 2
) +
annotate(
&quot;text&quot;,
x = 28 * 60,
y = 0.95,
label = &quot;Q3&quot;,
family = &quot;InputMono&quot;,
color = grey,
size = 2
) +
annotate(
&quot;text&quot;,
x = 13 * 60,
y = 0.95,
label = &quot;Q4&quot;,
family = &quot;InputMono&quot;,
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 = &quot;b&quot;,
size = 1.5
) +
geom_rug(
data = filter(data, home_scoring_play == 1),
color = foreground_color,
sides = &quot;t&quot;,
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 = &quot;InputMono&quot;,
background_color = background_color,
foreground_color = foreground_color
) +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(
title = str_interp(
&quot;${game_year} Week ${game_week}: ${away_team_abbr} at ${home_team_abbr}&quot;
),
subtitle = &quot;Custom win probability model compared to nflfastR WP (grey)&quot;,
caption = &quot;Data from nflfastR&quot;,
x = &quot;Quarters&quot;,
y = &quot;Home Win Probability&quot;
)
}
for (single_game_id in game_ids) {
plot &lt;-
plot_for_data(
filter(pbp_data, game_id == single_game_id),
logos,
foreground_color,
background_color
)
ggsave(
str_interp(&quot;wp-${single_game_id}.png&quot;),
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()

huangapple
  • 本文由 发表于 2023年2月18日 03:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定