ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

「2014 FIFA World Cup」「2018 FIFA World Cup」を 分析

「Copa do Mundo de Futebol FIFA Brasil 2014」「Чемпионат мира по футболу 2018」を 分析してみた。

データは、ここから入手。
https://www.whoscored.com/Statistics

こんな風な tab 区切りファイルに保存

Team	Rank	Rating	Shotsconceded 	Shots	Tackles	CaughtOffside	Blocks	Interception	Clearances	Save	Goals	Dribbles	PossessionLoss	AerialWon	AerialLost	Passes	KeyPpasses	Assists	Fouls	Fouled
Algeria	10	6.92	15.5	9	20.8	1.5	10.8	16.3	29.5	4.8	1.5	6.5	19.8	14.8	22	326	6.5	1.3	17.3	13.5
Argentina	4	7.12	11.1	15.4	19	1.4	12.5	14.1	26.7	2.4	0.9	12.3	24	10.4	9.7	461.3	10.4	0.3	11	16.6
Australia	30	6.39	11.3	9	12.3	1	12	15.3	21	2.7	1	10.3	24.4	12.7	10.7	401.7	7.3	0.7	16.7	13
...省略...
Switzerland	1	7.14	18.3	16.3	20	1.3	15.8	12	24.5	4.3	1.8	6.5	23.6	11	10.3	392	13	1.5	16.5	15.8
Uruguay	26	6.63	10.8	11.8	18.5	2.8	12.3	15.8	24.3	2.3	1	5.5	23.3	18.3	17.5	346	7.8	0.5	18.3	15.8
USA	11	6.91	23.5	11	20	1	17.1	13.5	38.5	5.8	1	9	17.5	15	16.3	385.3	5.8	0.8	12.3	14

R で得点の棒グラフを描画

# ggplot2 パッケージを使用
library(ggplot2)
# x軸=国名、y軸=1試合当たりの得点
g <- ggplot(d, aes(x = d$"ID", y = (d$"GoalFor" / d$"MatchesPlayed")))
g <- g + geom_bar(stat = "identity")
plot(g)

f:id:fornext1119:20180922220140p:plain

グラフを横倒し

# グラフを横倒し
g <- g + coord_flip()

f:id:fornext1119:20180922220353p:plain

得点順に並べる

# 得点順に並べる
g <- g + geom_bar(stat = "identity" ,aes(x=reorder(d$"ID",(d$"GoalFor" / d$"MatchesPlayed"))))

f:id:fornext1119:20180922220508p:plain

グラフにタイトルをつける

# グラフにタイトルをつける
g <- g + labs(title="1試合当たりの得点")
g <- g + xlab("Team")
g <- g + ylab("得点")

f:id:fornext1119:20180922220716p:plain

成績で色分け

# 成績で色分け
,fill=d$"MatchesPlayed"

f:id:fornext1119:20180922220839p:plain

試合数が離散値ではなく連続値として描画されているので、文字と認識させる

# 成績で色分け
,fill=as.character(d$"MatchesPlayed")

f:id:fornext1119:20180922221131p:plain

色が汚くなったので、マニュアル指定

# 色・凡例を指定
g <- g + scale_fill_manual(
    values=c("#74c0fc","#339af0","#1c7ed6","#1864ab")
   ,labels=c("Group Stage","Top 16","Top 8","Top 4")
)

f:id:fornext1119:20180922221311p:plain

凡例の位置を変更

# 凡例の位置を変更
g <- g + theme(legend.position=c(0.85,0.5))

f:id:fornext1119:20180922221409p:plain

フォントやテーマを指定

# Font を準備
windowsFonts(HGKAI=windowsFont("HG正楷書体-PRO"))
windowsFonts(COURIER=windowsFont("Courier New"))

# 成績で色分け
 ,fill=as.character(d$"MatchesPlayed")
 ,colour=as.character(d$"MatchesPlayed")

# 凡例の並び順を変更
g <- g + guides(fill=guide_legend(reverse = T))
g <- g + guides(colour=guide_legend(reverse = T))

g <- g + labs(fill="凡例")
g <- g + labs(colour="凡例")

# 色・凡例を指定
g <- g + scale_fill_manual(
    values=c("#f06595","#845ef7","#339af0","#20c997")
   ,labels=c("Group Stage","Top 16","Top 8","Top 4")
)
g <- g + scale_colour_manual(
    values=c("#f06595","#845ef7","#339af0","#20c997")
   ,labels=c("Group Stage","Top 16","Top 8","Top 4")
)
# フォントを指定
g <- g + theme_bw(
    base_size=10, 
    base_family="HGKAI"
)
g <- g + theme(axis.text=element_text(family="COURIER", size=8))

f:id:fornext1119:20180922221720p:plain

得失点差の棒グラフ

f:id:fornext1119:20180922222048p:plain

塗りつぶす必要はないんじゃないかと

# x軸=国名、y軸=1試合当たりの得失点差
g <- g + geom_point(
    aes(
        # 得失点差順に並べる
        x=reorder(d$"ID",((d$"GoalFor" - d$"GoalAgainst") / d$"MatchesPlayed"))
        # 成績で色分け
        ,colour=as.character(d$"MatchesPlayed")
    )
)

f:id:fornext1119:20180922222211p:plain

アノテーションで日本の成績を強調

これはかなり面倒くさい...

g <- g + annotate("text"
    , x = c(6,33)
    , y = c(-2.4,-1.3)
    , label = c("日本(2014)","日本(2018)") 
    , color="navy"
    , size=3.5 
    , angle=0
    ,family="HGKAI"
)
# Add arrow
g <- g + annotate("segment", 
    x = 6, 
    xend = 6, 
    y = -1.9, 
    yend = -1.4, 
    colour = "navy", 
    size=0.5, 
    arrow=arrow(
        angle = 20, 
        length = unit(0.1, "inches"),
        type = "open"
    )
)
g <- g + annotate("segment", 
    x = 33, 
    xend = 33, 
    y = -0.8, 
    yend = -0.3, 
    colour = "navy", 
    size=0.5, 
    arrow=arrow(
        angle = 20, 
        length = unit(0.1, "inches"),
        type = "open"
    )
)

f:id:fornext1119:20180922222749p:plain

repel で日本の成績を強調

d$"ID2" <- ifelse(d$"ID" == "Japan(14)", "日本(2014)", ifelse(d$"ID" == "Japan(18)", "日本(2018)", ""))

# ggrepel パッケージを使用
library("ggrepel")

g <- g + geom_label_repel(
    aes(label = d$"ID2") , 
    fill="#f06595", 
    colour="white",
    segment.color="#f06595" , 
    family="HGKAI",
    show.legend=F
)

f:id:fornext1119:20180922223045p:plain

点の大きさで得点を表現

,size=(( d$"GoalFor") / d$"MatchesPlayed")

f:id:fornext1119:20180922223257p:plain

タックル

f:id:fornext1119:20180922224315p:plain

クリア

f:id:fornext1119:20180922224647p:plain

シュート

f:id:fornext1119:20180922224834p:plain

ドリブル

f:id:fornext1119:20180922225144p:plain

ロングパス

f:id:fornext1119:20180922225457p:plain

ショートパス

f:id:fornext1119:20180922225651p:plain