如何正确地将图像添加到facet-wrapped ggplot中?我当前的输出将图像返回为单色轮廓

问题描述 投票:0回答:1

我正在尝试创建一个多面包裹的散点图,用于显示三个大联盟投手的投球运动数据。我唯一不明白的是如何将这三名球员的头像图像添加到每个投手的数据中。我当前的代码 1) 需要几分钟的时间来抓取/放置玩家头像,尽管只有三个图像,2) 将头像输出为红色轮廓。为什么图像会这样显示以及如何修复?下面是输出的代码和视觉效果。

library(baseballr)
library(dplyr)
library(tidyverse)
library(grid)
library(mlbplotR)
library(ggplot2)

# pull data for three different pitchers
senga <- baseballr::statcast_search_pitchers(start_date = "2023-03-30",
                                             end_date = "2023-04-03",
                                             pitcherid = 673540)
strider <- baseballr::statcast_search_pitchers(start_date = "2023-03-30",
                                               end_date = "2023-04-03",
                                               pitcherid = 675911)
kirby <- baseballr::statcast_search_pitchers(start_date = "2023-03-30",
                                             end_date = "2023-04-03",
                                             pitcherid = 669923)
# bind to one df
df <- rbind(senga,strider,kirby) %>%
  filter(pitch_type != 'PO')

player_cleaned_data <- all_data %>% 
  # Only keep rows with pitch movement readings and during the regular season
  filter(!is.na(pfx_x), !is.na(pfx_z),
                game_type == "R") %>% 
  mutate(pfx_x_in_pv = -12*pfx_x,
                pfx_z_in = 12*pfx_z)

# Make a named vector to scale pitch colors with
pitch_colors <- c("4-Seam Fastball" = "red",
                  "2-Seam Fastball" = "orange",
                  "Sinker" = "cyan",
                  "Cutter" = "firebrick",
                  "Fastball" = "gray",
                  "Curveball" = "blue",
                  "Knuckle Curve" = "pink",
                  "Slider" = "orange",
                  "Changeup" = "#4cbb17",
                  "Forkball" = "hotpink",
                  "Split-Finger" = "#FC6C85",
                  "Sweeper" = "coral",
                  "Knuckleball" = "black")

# Find unique pitch types to not have unnecessary pitches in legend
pitch_types <- unique(player_cleaned_data$pitch_name)

player_cleaned_data %>% 
  ggplot(aes(x = pfx_x_in_pv, y = pfx_z_in, color = pitch_name)) +
  geom_vline(xintercept = 0) +
  geom_hline(yintercept = 0) +
  geom_point(size = 2, alpha = 0.45) +
  # Scale the pitch colors to match what we defined above
  scale_color_manual(values = pitch_colors,
                     limits = pitch_types) +
  # Scale axes and add " to end of labels to denote inches
  scale_x_continuous(limits = c(-25,25),
                     breaks = seq(-20,20, 5),
                     labels = scales::number_format(suffix = "\"")) +
  scale_y_continuous(limits = c(-25,25),
                     breaks = seq(-20,20, 5),
                     labels = scales::number_format(suffix = "\"")) +
  theme(text = element_text(family = "Trebuchet MS"),
        plot.title = element_text(size = 20, face = 'bold'),
        axis.title = element_text(size = 12),
        legend.title = element_blank(),
        legend.text = element_text(size = 11),
        plot.caption = element_text(size = 10),
        legend.key = element_rect(fill = NA)) +
  coord_equal() +
  labs(title = "Pitch Movement Profiles",
       subtitle = "2023 MLB Season | Pitcher's POV",
       caption = "Data: Baseball Savant via baseballr", 
       x = "Horizontal Break",
       y = "Induced Vertical Break", color = "Pitch Name") +
  facet_wrap(~full_name) +
  mlbplotR::geom_mlb_headshots(aes(player_id = pitcher), x = 17, y = 17, width = .2)

r image ggplot2 scatter-plot facet-wrap
1个回答
0
投票

第一个问题是您正在映射

color
中的
mlbplotR::geom_mlb_headshots
aes。因此,头像会根据色阶进行着色。为了解决这个问题,我添加了
color=NULL
。其次,渲染时间长的问题是您为数据的每行添加一张图片,即多次添加图像。相反,您只需要减少数据,可以使用 data = ~distinct(.x, pitcher, player_name) 中的
mlbplotR::geom_mlb_headshots
来实现:
library(baseballr)
library(tidyverse)
library(grid)
library(mlbplotR)
library(ggplot2)

player_cleaned_data %>%
  ggplot(aes(x = pfx_x_in_pv, y = pfx_z_in, color = pitch_name)) +
  geom_vline(xintercept = 0) +
  geom_hline(yintercept = 0) +
  geom_point(size = 2, alpha = 0.45) +
  # Scale the pitch colors to match what we defined above
  scale_color_manual(
    values = pitch_colors,
    limits = pitch_types
  ) +
  # Scale axes and add " to end of labels to denote inches
  scale_x_continuous(
    limits = c(-25, 25),
    breaks = seq(-20, 20, 5),
    labels = scales::number_format(suffix = "\"")
  ) +
  scale_y_continuous(
    limits = c(-25, 25),
    breaks = seq(-20, 20, 5),
    labels = scales::number_format(suffix = "\"")
  ) +
  theme(
    text = element_text(family = "Trebuchet MS"),
    plot.title = element_text(size = 20, face = "bold"),
    axis.title = element_text(size = 12),
    legend.title = element_blank(),
    legend.text = element_text(size = 11),
    plot.caption = element_text(size = 10),
    legend.key = element_rect(fill = NA)
  ) +
  coord_equal() +
  labs(
    title = "Pitch Movement Profiles",
    subtitle = "2023 MLB Season | Pitcher's POV",
    caption = "Data: Baseball Savant via baseballr",
    x = "Horizontal Break",
    y = "Induced Vertical Break", color = "Pitch Name"
  ) +
  facet_wrap(~player_name) +
  mlbplotR::geom_mlb_headshots(
    data = ~distinct(.x, pitcher, player_name),
    aes(player_id = pitcher, color = NULL),
    x = 17, y = 17, width = .2
  )

© www.soinside.com 2019 - 2024. All rights reserved.