-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchord_2020.R
167 lines (150 loc) · 4.7 KB
/
chord_2020.R
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
library(tidyverse)
library(tweenr)
library(circlize)
library(migest)
library(magick)
##
## wikipedia data
##
w1 <- read_csv("./data/wiki_players.csv")
w2 <- read_csv("./data/wiki_colours.csv")
w3 <- read_csv("./data/wiki_comp.csv")
# club_country to nat_team data frame
d <- w1 %>%
filter(year == 2020) %>%
select(year, nat_team, club_country_harm, contains("alpha3")) %>%
rename(club_country = club_country_harm,
nat_alpha3 = nat_team_alpha3) %>%
replace_na(list(club_country = "No Club")) %>%
mutate(nat_team = fct_inorder(nat_team)) %>%
group_by(club_country, nat_team, year, club_alpha3, nat_alpha3) %>%
summarise(n = n()) %>%
ungroup() %>%
arrange(nat_team)
# nation codes and kit colours
n0 <- d %>%
select(contains("nat_")) %>%
distinct() %>%
left_join(w2, by = c("nat_alpha3" = "team_alpha3")) %>%
mutate(nat_team = as.character(nat_team)) %>%
rename(lab = nat_team,
alpha3 = nat_alpha3)
# nations of players clubs not in tournament
c0 <- d %>%
select(contains("club_")) %>%
distinct() %>%
filter(!(club_country %in% n0$lab)) %>%
mutate(kit_shirt = "transparent",
kit_shorts = "transparent",
kit_socks = "transparent",
club_country = fct_inorder(club_country),
club_country = fct_rev(club_country)) %>%
arrange(club_country) %>%
mutate(club_country = as.character(club_country)) %>%
rename(lab = club_country,
alpha3 = club_alpha3)
# label details
r <- n0 %>%
bind_rows(c0) %>%
mutate(gap = ifelse(kit_shirt == "transparent", 2.5, 1.5),
label = case_when(
lab == "Bosnia and Herzegovina" ~ "Bosnia",
lab == "North Macedonia" ~ "North\nMacedonia",
lab == "Czech Republic" ~ "Czech\nRepublic",
TRUE ~ lab
))
# plot into pdf
pdf(file = "./plot/euro_2020_chord.pdf", useDingbats = FALSE)
par(mar = rep(0, 4), bg = "grey40", lheight = 0.8)
circos.clear()
circos.par(track.margin = c(0.01, -0.01),
points.overflow.warning = FALSE,
gap.degree = 2.5,
start.degree = 90)
# plot the chord diagram
chordDiagram(
x = select(d, nat_team, club_country, n),
order = r$lab,
grid.col = r %>%
select(lab, kit_shirt) %>%
deframe(),
transparency = 0.1,
directional = -1,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow",
diffHeight = -0.02,
link.sort = TRUE,
link.largest.ontop = TRUE,
h.ratio = 0.6,
annotationTrack = "grid",
annotationTrackHeight = 0.02,
preAllocateTracks = list(track.height = 0.2),
)
circos.trackPlotRegion(
track.index = 1,
bg.border = NA,
panel.fun = function(x, y) {
s <- get.cell.meta.data("sector.index")
rr <- filter(r, lab == s)
xx <- get.cell.meta.data("xlim") %>%
mean()
flag_disp <- TRUE
if(is.na(rr$alpha3))
flag_disp <- FALSE
if(rr$alpha3 %in% c0$alpha3)
flag_disp <- FALSE
if(flag_disp){
flag <- rr$alpha3 %>%
paste0("./flag/", . ,".png") %>%
image_read()
circos.raster(image = flag, x = mean(xx), y = 0.2,
width = "0.4cm", facing = "clockwise", niceFacing = TRUE)
}
circos.text(x = xx, y = ifelse(flag_disp, 0.38, 0.1),
labels = rr$label, adj = c(0, 0.5),
facing = "clockwise", niceFacing = TRUE,
col = "white", cex = 0.8)
}
)
dev.off()
file.show("./plot/euro2020_chord.pdf")
library(magick)
p <- image_read_pdf("./plot/euro_2020_chord.pdf")
# pp <- image_read_pdf("./plot/euro_2020.pdf")
# pp
logo <- paste0("./logo/", 2020, ".png") %>%
image_read(density = 300) %>%
image_resize(geometry = "350x350") %>%
image_extent(geometry = "350x350", gravity = "north-east") %>%
image_border(color = "grey40")
# x1 <-
p %>%
image_composite(logo, gravity = "north-east") %>%
image_annotate(
text = "Euro 2020 Squads",
color = "white", size = 18, location = "+10+10"
) %>%
image_annotate(
text = "Leagues to national teams\nby @guyabelguyabel",
color = "white", size = 10, location = "+10+100"
) %>%
image_annotate(
color = "white", size = 10, gravity = "south-west", location = "+10+330",
text = "Details:"
) %>%
image_annotate(
color = "white", size = 6, gravity = "south-west", location = "+10+10",
text = "Colours based on the
shirt of each national
team. Chords represent
connections between the
country of a player’s club (at
the chord base) and their national
team (at the arrow head). Chord thickness
represents the number of players per club
country-national team combination. Created
in R. Data scraped from squad lists on Wikipedia:
https://en.wikipedia.org/wiki/UEFA_European_Championship"
) %>%
image_write(path = "./plot/euro_2020.png")
# p1