library(dplyr)
## Warning: パッケージ 'dplyr' はバージョン 4.3.3 の R の下で造られました
##
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## intersect, setdiff, setequal, union
library(readr)
## Warning: パッケージ 'readr' はバージョン 4.3.3 の R の下で造られました
library(lubridate)
## Warning: パッケージ 'lubridate' はバージョン 4.3.3 の R の下で造られました
##
## 次のパッケージを付け加えます: 'lubridate'
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## date, intersect, setdiff, union
library(ggplot2)
## Warning: パッケージ 'ggplot2' はバージョン 4.3.3 の R の下で造られました
library(plotly)
## Warning: パッケージ 'plotly' はバージョン 4.3.3 の R の下で造られました
##
## 次のパッケージを付け加えます: 'plotly'
## 以下のオブジェクトは 'package:ggplot2' からマスクされています:
##
## last_plot
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter
## 以下のオブジェクトは 'package:graphics' からマスクされています:
##
## layout
data <- read_csv("results.csv")
## Rows: 44691 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): home_team, away_team, tournament, city, country
## dbl (2): home_score, away_score
## lgl (1): neutral
## date (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(data)
## # A tibble: 6 × 9
## date home_team away_team home_score away_score tournament city country
## <date> <chr> <chr> <dbl> <dbl> <chr> <chr> <chr>
## 1 1872-11-30 Scotland England 0 0 Friendly Glasg… Scotla…
## 2 1873-03-08 England Scotland 4 2 Friendly London England
## 3 1874-03-07 Scotland England 2 1 Friendly Glasg… Scotla…
## 4 1875-03-06 England Scotland 2 2 Friendly London England
## 5 1876-03-04 Scotland England 3 0 Friendly Glasg… Scotla…
## 6 1876-03-25 Scotland Wales 4 0 Friendly Glasg… Scotla…
## # ℹ 1 more variable: neutral <lgl>
tournament_counts <- data %>%
group_by(tournament) %>%
summarise(match_count = n()) %>%
arrange(desc(match_count))
tournaments_800_plus <- tournament_counts %>%
filter(match_count >= 800)
tournaments_800_plus
## # A tibble: 6 × 2
## tournament match_count
## <chr> <int>
## 1 Friendly 17647
## 2 FIFA World Cup qualification 7878
## 3 UEFA Euro qualification 2677
## 4 African Cup of Nations qualification 1976
## 5 FIFA World Cup 964
## 6 Copa América 841
knitr::kable(tournaments_800_plus)
| tournament | match_count |
|---|---|
| Friendly | 17647 |
| FIFA World Cup qualification | 7878 |
| UEFA Euro qualification | 2677 |
| African Cup of Nations qualification | 1976 |
| FIFA World Cup | 964 |
| Copa América | 841 |
data <- data %>%
mutate(year = year(ymd(date)))
data_800_plus <- data %>%
filter(tournament %in% tournaments_800_plus$tournament)
annual_match_counts <- data_800_plus %>%
group_by(year, tournament) %>%
summarise(match_count = n()) %>%
arrange(year)
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
ggplot(annual_match_counts, aes(x = year, y = match_count, color = tournament)) +
geom_line() +
labs(title = "Annual Match Counts by Tournament",
x = "Year",
y = "Number of Matches",
color = "Tournament") +
theme_minimal()
このグラフからcopa americaが世界最古のナショナルチームによるサッカーの大陸選手権大会であることが分かった。そして最大試合数を誇るのはFIFA World Cupであることもわかる。
data <- data %>%
mutate(result = case_when(
home_score > away_score ~ "Home Win",
home_score < away_score ~ "Away Win",
TRUE ~ "Draw"
))
home_stats <- data %>%
group_by(home_team) %>%
summarise(
home_games = n(),
home_wins = sum(result == "Home Win"),
home_win_rate = home_wins / home_games
) %>%
filter(home_games > 200) %>%
arrange(desc(home_win_rate))
away_stats <- data %>%
group_by(away_team) %>%
summarise(
away_games = n(),
away_wins = sum(result == "Away Win"),
away_win_rate = away_wins / away_games
) %>%
filter(away_games > 200) %>%
arrange(desc(away_win_rate))
home_stats
## # A tibble: 92 × 4
## home_team home_games home_wins home_win_rate
## <chr> <int> <int> <dbl>
## 1 Brazil 597 428 0.717
## 2 Spain 381 259 0.680
## 3 Argentina 577 383 0.664
## 4 Egypt 407 263 0.646
## 5 Ivory Coast 287 184 0.641
## 6 Nigeria 291 185 0.636
## 7 Iran 296 187 0.632
## 8 Italy 467 291 0.623
## 9 England 527 328 0.622
## 10 Ghana 303 188 0.620
## # ℹ 82 more rows
away_stats
## # A tibble: 96 × 4
## away_team away_games away_wins away_win_rate
## <chr> <int> <int> <dbl>
## 1 Germany 461 247 0.536
## 2 Brazil 429 229 0.534
## 3 England 526 273 0.519
## 4 Spain 356 169 0.475
## 5 Iran 231 106 0.459
## 6 South Korea 406 181 0.446
## 7 Netherlands 382 169 0.442
## 8 Russia 408 176 0.431
## 9 Japan 277 117 0.422
## 10 Australia 239 100 0.418
## # ℹ 86 more rows
ブラジルが総合的に高い勝率を誇っているが遠征での勝率はドイツに負けている。自スタジアムでは全体的に高く遠征は高くて5割に近い結果になっている。
# 勝敗の判定
data <- data %>%
mutate(result = case_when(
home_score > away_score ~ "Home Win",
home_score < away_score ~ "Away Win",
TRUE ~ "Draw"
))
# 各チームのホームゲームでの勝率
home_stats <- data %>%
group_by(home_team) %>%
summarise(
home_games = n(),
home_wins = sum(result == "Home Win"),
home_win_rate = home_wins / home_games
)
# 各チームのアウェイゲームでの勝率
away_stats <- data %>%
group_by(away_team) %>%
summarise(
away_games = n(),
away_wins = sum(result == "Away Win"),
away_win_rate = away_wins / away_games
)
# 両方のデータをマージ
merged_stats <- home_stats %>%
inner_join(away_stats, by = c("home_team" = "away_team")) %>%
rename(team = home_team) %>%
filter(home_games > 200 & away_games > 200)
# 全体での勝率を計算
merged_stats <- merged_stats %>%
mutate(overall_win_rate = (home_wins + away_wins) / (home_games + away_games))
# ホーム・アウェイ勝率をプロット
p <- ggplot(merged_stats, aes(x = home_win_rate, y = away_win_rate, label = team, color = overall_win_rate)) +
geom_text(size = 3, hjust = 0.5, vjust = 0.5) +
scale_color_continuous(low = "blue", high = "red", name = "Overall Win Rate") +
labs(title = "Home vs. Away Win Rates by Team",
x = "Home Win Rate",
y = "Away Win Rate") +
theme_minimal()
# インタラクティブプロットに変換
p_interactive <- ggplotly(p, tooltip = c("team", "home_win_rate", "away_win_rate", "overall_win_rate"))
p_interactive
このグラフでもわかる通りブラジルはどちらも強くスペインはホームゲームでは強いがアウェイゲームは弱いことが分かる。逆にイギリスとドイツはともにアウェイゲームが強いがホームゲームは他の国に劣っている。
# 10年ごとの年代を抽出
data <- data %>%
mutate(decade = floor(year / 10) * 10)
# 各年代ごとの勝率を計算
decade_stats <- data %>%
group_by(decade, home_team) %>%
summarise(
games = n(),
wins = sum(result == "Home Win"),
win_rate = wins / games
) %>%
ungroup() %>%
filter(games > 60)
## `summarise()` has grouped output by 'decade'. You can override using the
## `.groups` argument.
ggplot(decade_stats, aes(x = factor(decade), y = win_rate, group = home_team, color = home_team)) +
geom_line() +
geom_point() +
labs(title = "Win Rate by Decade",
x = "Decade",
y = "Win Rate") +
scale_color_viridis_d() +
theme_minimal() +
theme(legend.position = "none") +
geom_text(aes(label = home_team), size = 3, hjust = 0.5, vjust = -0.5, check_overlap = TRUE)
わかりにくいグラフになってしまったがブラジルが高い勝率を保っていること、マラウイが急激に低下してしまったこと、エジプトが急激に勝率を上げたことがこのグラフからわかることができた