简介
今天介绍一个与足球相关的 R 包。如果你也对体育感兴趣,可以看看:CRAN任务视图:Sports Analytics 。
ggsoccer[1]包可以基于 ggplot[2] 图层,绘制足球相关数据。
安装
通过 CRAN 下载
install.packages("ggsoccer")
或者通过 Github 下载
# install.packages("remotes") remotes::install_github("torvaney/ggsoccer")
使用教程
通过 annotate_pitch()
和 theme_pitch()
即可得到
library(ggplot2) library(ggsoccer) ggplot() + annotate_pitch() + theme_pitch()
其实,你也可以通过 ggplot 直接复现这个图形。文末给出完整版代码,供读者参考。
在上面的基础上,给出两个常用的例子:
绘制传球路线
添加传球路线数据,如下所示
pass_data <- data.frame(x = c(24, 18, 64, 78, 53), y = c(43, 55, 88, 18, 44), x2 = c(34, 44, 81, 85, 64), y2 = c(40, 62, 89, 44, 28))
通过 geom_segment()
将传球数据进行绘制。direction_label()
添加图底部标签。annotate_pitch()
中可以修改足球场颜色。
ggplot(pass_data) + annotate_pitch(colour = "white", fill = "#3ab54a") + geom_segment(aes(x = x, y = y, xend = x2, yend = y2), arrow = arrow(length = unit(0.25, "cm"), type = "closed")) + theme_pitch() + direction_label()
如果你想将图形旋转 90 度,并只展示半个足球场。可以配合 coord_flip(xlim = c(49, 101))
和 scale_y_reverse()
使用。
绘制投射情况
接下来,再给一个相似的例子,展示某个队伍的投射点位以及是否射中。
df <- data.frame(x = rnorm(20, 80, 10), y = rnorm(20, 50, 20), Shot = sample(c("In", "Out"), 40, replace = TRUE))
此时,使用 geom_point()
添加投射点,根据 fill = Shot
进行填充分类。配合 coord_cartesian(xlim = c(45, 105))
展示水平的半个足球场。
library(viridis) ggplot(df) + annotate_pitch(colour = "white", fill = "#3ab54a") + geom_point(aes(x = x, y = y, fill = Shot), shape = 21, size = 4) + coord_cartesian(xlim = c(45, 105))+ scale_fill_viridis(discrete=T) + theme_pitch() + theme(panel.background = element_rect(fill = "#3ab54a"))
ggplot 复现版本
ggplot()+ geom_rect(aes(xmin = -10, xmax = 100, ymin= -10, ymax = 130), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ ### Background outline geom_rect(aes(xmin = 0, xmax = 90, ymin= 0, ymax = 120), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ ###Sideline, Endline geom_rect(aes(xmin = 0, xmax = 90, ymin= 0, ymax = 60), fill = NA , colour = "#FFFFFF", size = 0.5)+ #Halfway geom_rect(aes(xmin = 24.85, xmax = 65.15, ymin= 0, ymax = 16.5), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ #lower 18 yard box geom_rect(aes(xmin = 35.85, xmax = 54.15, ymin= 0, ymax = 5.5), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ # Lower 5 yard box geom_rect(aes(xmin = 24.85, xmax = 65.15, ymin= 103.5, ymax = 120), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ #upper 18 yard box geom_rect(aes(xmin = 35.85, xmax = 54.15, ymin= 114.5, ymax = 120), fill = "#3ab54a", colour = "#FFFFFF", size = 0.5)+ #upper 5 yard box geom_curve(aes(x = 35.85, y = 16.5, xend = 54.15, yend = 16.5), curvature = -0.6, colour = "#FFFFFF")+ #lower D geom_curve(aes(x = 35.85, y = 103.5, xend = 54.15, yend = 103.5), curvature = .6, colour = "#FFFFFF")+ #upper D ggforce::geom_circle(aes(x0=45, y0=60, r= 9.15), colour = "#FFFFFF")+ ##Halfway circle # coord_flip(xlim = c(49, 101)) + coord_flip() + scale_y_reverse() + theme(rect = element_blank(), line = element_blank(), text = element_blank())
小编有话说
本文介绍了绘制足球数据的 ggsoccer 包。如果你也对足球感兴趣,并想做一些数据分析。那么你可以从下面包中获取感兴趣的足球数据,然后基于 ggsoccer 包进行可视化。例如:
- worldfootballR[3] 提供来自许多热门网站的足球数据。
- 欧洲足球数据可通过 engsoccerdata[4] 包获得,数据可追溯至 1871 年。
- socceR[5]提供评估足球预测和模拟足球比赛和锦标赛结果的功能。
- footballpenaltiesBL[6] 可以分析 德国男子德甲联赛[7] 从 1963-64 赛季到 2016-17 赛季的数据。
- footBayes[8] 包含一些拟合足球的模型(例如:双泊松、双变量泊松、Skellam、Student's t)的函数。拟合方法是包括 Hamiltonian 蒙特卡罗法和最大似然估计法。该软件包还提供了可视化团队实力和预测比赛结果的工具。
- itschalledsoccer[9] 允许通过 API[10] 访问美式足球(MLS、NWSL 和 USL)数据 。
参考资料
[1]
ggsoccer: https://torvaney.github.io/ggsoccer/
[2]
ggplot: https://ggplot2.tidyverse.org/
[3]
worldfootballR: https://cran.r-project.org/web/packages/worldfootballR/index.html
[4]
engsoccerdata: https://cran.r-project.org/web/packages/engsoccerdata/index.html
[5]
socceR: https://cran.r-project.org/web/packages/socceR/index.html
[6]
footballpenaltiesBL: https://cran.r-project.org/web/packages/footballpenaltiesBL/index.html
[7]
德国男子德甲联赛: https://www.bundesliga.com/
[8]
footBayes: https://cran.r-project.org/web/packages/footBayes/index.html
[9]
itschalledsoccer: https://cran.r-project.org/web/packages/itscalledsoccer/index.html
[10]