Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Created June 14, 2018 11:03
Show Gist options
  • Save PaulC91/e767ca4f0c4335e6e0d2f71eb7cc98cc to your computer and use it in GitHub Desktop.
Save PaulC91/e767ca4f0c4335e6e0d2f71eb7cc98cc to your computer and use it in GitHub Desktop.
network graph of world cup players countries and clubs
library(rvest)
library(tidyverse)
library(tidygraph)
library(ggraph)
wc_squads <- read_html("https://en.wikipedia.org/wiki/2018_FIFA_World_Cup_squads") %>%
html_table()
teams <- c("Egypt", "Russia", "Saudi Arabia", "Uruguay",
"Iran", "Morocco", "Portugal", "Spain",
"Australia", "Denmark", "France", "Peru",
"Argentina", "Croatia", "Iceland", "Nigeria",
"Brazil", "Costa Rica", "Serbia", "Switzerland",
"Germany", "Mexico", "South Korea", "Sweden",
"Belgium", "England", "Panama", "Tunisia",
"Colombia", "Japan", "Poland", "Senegal")
df <-
map_df(1:32, ~ {
wc_squads[[.x]] %>%
mutate(Country = teams[.x]) %>%
select(Player, Country, Club)
}) %>%
rownames_to_column("id")
country_clubs <- df %>%
select(Country, Club) %>%
group_by_all() %>%
summarise(weight = n()) %>%
filter(Country %in% c("Brazil", "France", "Germany", "Spain", "Belgium"))
wc_network <- as_tbl_graph(country_clubs) %>%
mutate(type = if_else(name %in% df$Country, "Country", "Club"))
ggraph(wc_network) +
geom_edge_arc(aes(width = weight), alpha = .25, colour = "white") +
scale_edge_width(range = c(0.2, 2)) +
geom_node_point(aes(colour = type, size = type), show.legend = FALSE) + #
scale_size_manual(values = c(2, 10)) +
geom_node_text(aes(label = name), family = "Iosevka", colour = "white",
repel = TRUE, size = 3, show.legend = FALSE) +
theme_graph(background = "#2E3440", text_colour = "white",
base_family = "Iosevka", foreground = NA) +
labs(title = "World Cup 2018 | Club Country Network",
subtitle = "Belgium, Brazil, France, Germany, Spain",
caption = "@paulcampbell91 | Source: Wikipedia",
edge_width = "Players")
@stewartli
Copy link

learn something new. thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment