鶴見教育工学研究所

baseballrパッケージによる大谷翔平選手の投球データの可視化

Tags:

RでMLBのMVPプレイヤーのホームラン時の投球コースと球種を可視化するという記事がありました。自分でも、教材を作る際にbaseballrパッケージで大谷翔平選手の投球データ (2023年) を取得して、可視化したのを思い出して、コードだけ掲載します。

ライブラリの読み込みなど

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
library(tidyverse)
library(lubridate)
library(scales)
library(ggthemes)
options(dplyr.width = Inf, scipen = 1, digits = 4)
theme_set(theme_fivethirtyeight(base_family = "Hiragino Kaku Gothic ProN", base_size = 14))
theme_update(axis.title = element_text(), plot.title = element_text(size = 14))
library(baseballr)
library(khroma)
library(ggrepel)

データの取得

1
2
3
4
5
6
ohtani_pitch <- statcast_search_pitchers(start_date="2023-01-01", end_date="2023-12-31", pitcherid="660271")
ohtani_pitch <- ohtani_pitch |>
    mutate(pitch_type = fct_infreq(pitch_type)) |>
    mutate(pitch_type = fct_recode(pitch_type, "n/a" = ""))

head(ohtani_pitch)
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
## # A tibble: 6 × 113
##   pitch_type game_date  release_speed release_pos_x release_pos_z player_name   
##   <fct>      <date>             <dbl>         <dbl>         <dbl> <chr>         
## 1 FF         2023-08-23          94.2         -1.98          5.78 Ohtani, Shohei
## 2 ST         2023-08-23          76.1         -2.01          5.74 Ohtani, Shohei
## 3 ST         2023-08-23          77.2         -2.05          5.6  Ohtani, Shohei
## 4 FS         2023-08-23          90.1         -1.94          5.69 Ohtani, Shohei
## 5 FS         2023-08-23          90           -1.83          5.76 Ohtani, Shohei
## 6 ST         2023-08-23          79.8         -2.17          5.7  Ohtani, Shohei
##   batter pitcher events      description     spin_dir spin_rate_deprecated
##    <dbl>   <dbl> <chr>       <chr>           <lgl>    <lgl>               
## 1 687952  660271 ""          foul            NA       NA                  
## 2 687952  660271 ""          swinging_strike NA       NA                  
## 3 687952  660271 ""          swinging_strike NA       NA                  
## 4 687952  660271 ""          ball            NA       NA                  
## 5 687952  660271 ""          blocked_ball    NA       NA                  
## 6 458015  660271 "field_out" hit_into_play   NA       NA                  
##   break_angle_deprecated break_length_deprecated  zone
##   <lgl>                  <lgl>                   <dbl>
## 1 NA                     NA                          5
## 2 NA                     NA                         14
## 3 NA                     NA                          6
## 4 NA                     NA                         14
## 5 NA                     NA                         14
## 6 NA                     NA                         11
##   des                                                                           
##   <chr>                                                                         
## 1 Christian Encarnacion-Strand grounds into a double play, third baseman Eduard…
## 2 Christian Encarnacion-Strand grounds into a double play, third baseman Eduard…
## 3 Christian Encarnacion-Strand grounds into a double play, third baseman Eduard…
## 4 Christian Encarnacion-Strand grounds into a double play, third baseman Eduard…
## 5 Christian Encarnacion-Strand grounds into a double play, third baseman Eduard…
## 6 Joey Votto pops out to shortstop Andrew Velazquez.                            
##   game_type stand p_throws home_team away_team type  hit_location bb_type balls
##   <chr>     <chr> <chr>    <chr>     <chr>     <chr>        <int> <chr>   <int>
## 1 R         R     R        LAA       CIN       S               NA ""          2
## 2 R         R     R        LAA       CIN       S               NA ""          2
## 3 R         R     R        LAA       CIN       S               NA ""          2
## 4 R         R     R        LAA       CIN       B               NA ""          1
## 5 R         R     R        LAA       CIN       B               NA ""          0
## 6 R         L     R        LAA       CIN       X                6 "popup"     0
##   strikes game_year pfx_x pfx_z plate_x plate_z on_3b on_2b  on_1b outs_when_up
##     <int>     <int> <dbl> <dbl>   <dbl>   <dbl> <dbl> <dbl>  <dbl>        <int>
## 1       2      2023 -0.61  1.05   -0.11    2.14    NA    NA 668715            1
## 2       1      2023  1.2   0.09    0.89    2.39    NA    NA 668715            1
## 3       0      2023  0.75  0.55    0.8     2.11    NA    NA 668715            1
## 4       0      2023 -1.41  0.58    0.59    1.01    NA    NA 668715            1
## 5       0      2023 -1.36  0.72    1.29    0.18    NA    NA 668715            1
## 6       0      2023  0.25  1.12   -1.03    2.98    NA    NA 668715            0
##   inning inning_topbot  hc_x  hc_y tfs_deprecated tfs_zulu_deprecated umpire
##    <dbl> <chr>         <dbl> <dbl> <lgl>          <lgl>               <lgl> 
## 1      2 Top            NA     NA  NA             NA                  NA    
## 2      2 Top            NA     NA  NA             NA                  NA    
## 3      2 Top            NA     NA  NA             NA                  NA    
## 4      2 Top            NA     NA  NA             NA                  NA    
## 5      2 Top            NA     NA  NA             NA                  NA    
## 6      2 Top            98.3  137. NA             NA                  NA    
##   sv_id   vx0   vy0     vz0     ax    ay    az sz_top sz_bot hit_distance_sc
##   <lgl> <dbl> <dbl>   <dbl>  <dbl> <dbl> <dbl>  <dbl>  <dbl>           <dbl>
## 1 NA     6.27 -137. -6.07    -9.09  33.0 -17.8   3.36   1.55             211
## 2 NA     3.93 -111.  0.0236   8.87  24.1 -31.8   3.36   1.55              NA
## 3 NA     4.74 -112. -1.35     5.24  23.7 -27.5   3.36   1.55              NA
## 4 NA     9.37 -131. -6.85   -18.2   29.7 -24.2   3.39   1.53              NA
## 5 NA    10.7  -130. -9.45   -17.7   27.0 -22.1   3.49   1.53              NA
## 6 NA     2.05 -116. -1.32     1.90  22.6 -22.1   3.4    1.6              175
##   launch_speed launch_angle effective_speed release_spin_rate release_extension
##          <dbl>        <dbl>           <dbl>             <dbl>             <dbl>
## 1         73.8           32            94.6              2289               6.8
## 2         NA             NA            75.7              1217               6.5
## 3         NA             NA            77.2              2188               6.7
## 4         NA             NA            90.5              1000               6.9
## 5         NA             NA            90.8              1045               7  
## 6         75.3           61            80.2              2191               6.6
##   game_pk fielder_2 fielder_3 fielder_4 fielder_5 fielder_6 fielder_7 fielder_8
##     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
## 1  716890    681351    592273    650859    500871    623205    545341    666160
## 2  716890    681351    592273    650859    500871    623205    545341    666160
## 3  716890    681351    592273    650859    500871    623205    545341    666160
## 4  716890    681351    592273    650859    500871    623205    545341    666160
## 5  716890    681351    592273    650859    500871    623205    545341    666160
## 6  716890    681351    592273    650859    500871    623205    545341    666160
##   fielder_9 release_pos_y estimated_ba_using_speedangle
##       <dbl>         <dbl>                         <dbl>
## 1    592669          53.6                        NA    
## 2    592669          54.0                        NA    
## 3    592669          53.8                        NA    
## 4    592669          53.6                        NA    
## 5    592669          53.5                        NA    
## 6    592669          53.9                         0.017
##   estimated_woba_using_speedangle woba_value woba_denom babip_value iso_value
##                             <dbl>      <dbl>      <int>       <int>     <int>
## 1                          NA             NA         NA          NA        NA
## 2                          NA             NA         NA          NA        NA
## 3                          NA             NA         NA          NA        NA
## 4                          NA             NA         NA          NA        NA
## 5                          NA             NA         NA          NA        NA
## 6                           0.015          0          1           0         0
##   launch_speed_angle at_bat_number pitch_number pitch_name      home_score
##                <int>         <dbl>        <dbl> <chr>                <dbl>
## 1                 NA            11            5 4-Seam Fastball          2
## 2                 NA            11            4 Sweeper                  2
## 3                 NA            11            3 Sweeper                  2
## 4                 NA            11            2 Split-Finger             2
## 5                 NA            11            1 Split-Finger             2
## 6                  3            10            1 Sweeper                  2
##   away_score bat_score fld_score post_away_score post_home_score post_bat_score
##        <dbl>     <dbl>     <dbl>           <dbl>           <dbl>          <dbl>
## 1          0         0         2               0               2              0
## 2          0         0         2               0               2              0
## 3          0         0         2               0               2              0
## 4          0         0         2               0               2              0
## 5          0         0         2               0               2              0
## 6          0         0         2               0               2              0
##   post_fld_score if_fielding_alignment of_fielding_alignment spin_axis
##            <dbl> <chr>                 <chr>                     <dbl>
## 1              2 Standard              Standard                    204
## 2              2 Standard              Standard                     65
## 3              2 Standard              Standard                     69
## 4              2 Standard              Standard                    317
## 5              2 Standard              Standard                    191
## 6              2 Standard              Standard                    105
##   delta_home_win_exp delta_run_exp bat_speed swing_length
##                <dbl>         <dbl> <lgl>     <lgl>       
## 1              0             0     NA        NA          
## 2              0            -0.064 NA        NA          
## 3              0            -0.072 NA        NA          
## 4              0             0.063 NA        NA          
## 5              0             0.042 NA        NA          
## 6              0.036        -0.361 NA        NA          
##   estimated_slg_using_speedangle delta_pitcher_run_exp hyper_speed
##                            <dbl>                 <dbl>       <dbl>
## 1                         NA                     0              88
## 2                         NA                     0.064          NA
## 3                         NA                     0.072          NA
## 4                         NA                    -0.063          NA
## 5                         NA                    -0.042          NA
## 6                          0.019                 0.361          88
##   home_score_diff bat_score_diff home_win_exp bat_win_exp age_pit_legacy
##             <int>          <int>        <dbl>       <dbl>          <int>
## 1               2             -2        0.703       0.297             28
## 2               2             -2        0.703       0.297             28
## 3               2             -2        0.703       0.297             28
## 4               2             -2        0.703       0.297             28
## 5               2             -2        0.703       0.297             28
## 6               2             -2        0.667       0.333             28
##   age_bat_legacy age_pit age_bat n_thruorder_pitcher
##            <int>   <int>   <int>               <int>
## 1             23      29      24                   1
## 2             23      29      24                   1
## 3             23      29      24                   1
## 4             23      29      24                   1
## 5             23      29      24                   1
## 6             39      29      40                   1
##   n_priorpa_thisgame_player_at_bat pitcher_days_since_prev_game
##                              <int>                        <int>
## 1                                0                           14
## 2                                0                           14
## 3                                0                           14
## 4                                0                           14
## 5                                0                           14
## 6                                0                           14
##   batter_days_since_prev_game pitcher_days_until_next_game
##                         <int>                        <int>
## 1                           1                           NA
## 2                           1                           NA
## 3                           1                           NA
## 4                           1                           NA
## 5                           1                           NA
## 6                           1                           NA
##   batter_days_until_next_game api_break_z_with_gravity api_break_x_arm
##                         <int>                    <dbl>           <dbl>
## 1                           0                     1.54            0.61
## 2                           0                     3.93           -1.2 
## 3                           0                     3.33           -0.75
## 4                           0                     2.27            1.41
## 5                           0                     2.11            1.36
## 6                          18                     2.47           -0.25
##   api_break_x_batter_in arm_angle
##                   <dbl>     <dbl>
## 1                  0.61      36.7
## 2                 -1.2       33.4
## 3                 -0.75      34  
## 4                  1.41      37.6
## 5                  1.36      34.8
## 6                  0.25      33.1

球速の分布

1
2
3
ggplot(ohtani_pitch, aes(x = effective_speed)) +
    geom_histogram(fill = fivethirtyeight_pal()(1), colour = "white") +
    labs(title = "大谷翔平投手の投球速度の分布 (2023年)", x = "球速 (マイル)", y = "度数")
1
2
3
4
5
ggplot(ohtani_pitch, aes(x = effective_speed, y = pitch_type, fill = pitch_type)) +
    stat_boxplot(geom = "errorbar") +
    geom_boxplot() +
#    scale_fill_light() +
    labs(title = "大谷翔平投手の球種別の投球速度分布 (2023年)", x = "球速 (マイル)", y = "")
1
2
3
4
ggplot(ohtani_pitch, aes(x = effective_speed, y = pitch_type, fill = pitch_type)) +
    geom_violin() +
    scale_fill_light() +
    labs(title = "大谷翔平投手の球種別の投球速度分布 (2023年)", x = "球速 (マイル)", y = "")

球種別投球数

1
2
3
ggplot(ohtani_pitch, aes(x = pitch_type)) +
    geom_bar(fill = colour("light")(length(levels(ohtani_pitch$pitch_type)))) +
    labs(title = "大谷翔平投手の球種別投球数 (2023年)", x = "球種", y = "度数")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
ohtani_pitch_percentage <- ohtani_pitch |>
    group_by(pitch_type) |>
    summarise(prop = n() / nrow(ohtani_pitch)) |>
    mutate(labels = percent(prop), csum = rev(cumsum(rev(prop))), 
         pos = prop/2 + lead(csum, 1), pos = if_else(is.na(pos), prop/2, pos)) |>
    arrange(desc(prop))

ggplot(ohtani_pitch_percentage, aes(x = "", y = prop, fill = pitch_type)) +
    geom_col(colour = "black") +
    geom_label_repel(aes(y = pos, label = labels), size = 4.5, nudge_x = 0.7, show.legend = FALSE) +
    coord_polar(theta = "y", direction = -1) +
    scale_fill_light() +
    labs(title = "大谷翔平投手の球種別投球数 (2023年)", x = "球種", y = "度数", fill = "球種") +
    theme(axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank())
1
2
3
4
5
6
ggplot(ohtani_pitch_percentage, aes(x = prop, y = "", fill = pitch_type)) +
    geom_bar(position = position_fill(), stat = "identity") +
    geom_label_repel(aes(x = pos, label = labels), size = 4.5, nudge_y = 0.6, show.legend = FALSE) +
    scale_fill_light() +
    scale_x_reverse() +
    labs(title = "大谷翔平投手の球種別投球数 (2023年)", x = "球種", y = "度数", fill = "球種")

投球マップ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ohtani_pitch_position <- ohtani_pitch |>
    select(plate_x, plate_z, pitch_type) |>
    mutate(plate_x = plate_x * 30.48, plate_z = plate_z * 30.48)

ggplot(ohtani_pitch_position, aes(x = plate_x, y = plate_z, colour = pitch_type)) +
    annotate(geom = "rect", xmin = -28, xmax = 28, ymin = 40, ymax = 105, fill = "black", alpha = 0.3) +
    geom_point(alpha = 0.4, size = 2) +
    scale_x_continuous(breaks = seq(-150, 120, by = 30)) +
    scale_y_continuous(breaks = seq(-30, 200, by = 30)) +
    labs(title = "大谷翔平投手の投球マップ (2023年)", x = "", y = "")