英超联赛数据可视化

英超联赛数据可视化

本文是How did we get here? Three different Premier League stories.的学习笔记,讲述了如何使用engsoccerdata包中1888年——2017年英超联赛的数据和plotly包绘图。

整理数据:

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
# engsoccerdata包:包含了1888年——2017年所有四大级足球比赛的结果。
devtools::install_github('jalapic/engsoccerdata')
library(tidyverse)
library(engsoccerdata)
library(plotly)

team <- england %>%
gather(team, club, 3:4) %>%
mutate(win = ifelse(
team == 'home' & result == 'H', 3, ifelse(
team == 'visitor' & result == 'A', 3, ifelse(
result == 'D', 1, 0
)
)
))

tables <- team %>%
group_by(division,
Season,
club) %>%
summarise(points = sum(win)) %>%
arrange(division, Season, desc(points)) %>%
group_by(Season) %>%
mutate(position = row_number())

teams <- tables %>%
filter(club %in% c('Arsenal','Burnley','Leicester City')) %>%
ungroup()

current <- data.frame(
division = c(1, 1, 1),
Season = c(2018, 2018, 2018),
club = c('Arsenal', 'Burnley', 'Leicester City'),
points = c(48, 43, 40),
position = c(6, 7, 8))

teams <- rbind(teams, current)

绘图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
timeline <- ggplot(
teams,
aes(x = Season,
y = position,
color = club)) +
geom_line(size = 0.5) +
geom_point(alpha = 0.35,
cex = 0.75) +
scale_x_continuous(
position = c('top'),
breaks = seq(1888, 2018, by = 10)) +
scale_y_reverse(lim = c(92, 1)) +
hrbrthemes::theme_ipsum(
base_family = 'STSongti-SC-Bold'
) +
scale_color_brewer(palette = 'Set2') +
labs(x = '赛季',
y = '场上位置',
title = '三只球队',
caption = '数据来源:engsoccerdata包')

(p <- ggplotly(timeline, tooltip = c('Season', 'club', 'position')))
htmlwidgets::saveWidget(p, "英超联赛数据可视化.html")

JS图表:

英超联赛数据可视化.html

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×