Skip to content

Instantly share code, notes, and snippets.

@Ryo-N7
Last active January 4, 2019 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ryo-N7/4b7bbe0d32a4d45e68f20e359a01ff0d to your computer and use it in GitHub Desktop.
Save Ryo-N7/4b7bbe0d32a4d45e68f20e359a01ff0d to your computer and use it in GitHub Desktop.
Asian Cup champions!
pacman::p_load(tidyverse, scales, glue, extrafont, rvest, polite)
# Roboto Condensed font
loadfonts()
acup_url <- "https://en.wikipedia.org/wiki/AFC_Asian_Cup"
# responsible web-scraping ftw!
session <- bow(acup_url)
acup_winners_raw <- scrape(session) %>%
html_nodes("#mw-content-text > div > table:nth-child(30)") %>%
html_table() %>%
flatten_df()
# clean
acup_winners_clean <- acup_winners_raw %>%
janitor::clean_names() %>%
slice(1:8) %>%
select(-fourth_place, -total_top_four) %>%
separate(winners, into = c("first_num", "first_place_year"), sep = " ", extra = "merge") %>%
separate(runners_up, into = c("second_num", "second_place_year"), sep = " ", extra = "merge") %>%
separate(third_place, into = c("third_num", "third_place_year"), sep = " ", extra = "merge") %>%
mutate_all(funs(str_replace_all(., "–", "0"))) %>%
mutate_at(vars(contains("num")), funs(as.numeric)) %>%
mutate(team = if_else(team == "Israel1", "Israel", team)) %>%
gather(key = "key", value = "value", -team,
-first_place_year, -second_place_year, -third_place_year) %>%
mutate(key = case_when(
key == "first_num" ~ "Champions",
key == "second_num" ~ "Runners-up",
key == "third_num" ~ "Third Place"
),
key = key %>% fct_relevel(c("Champions", "Runners-up", "Third Place"))) %>%
# hack-ish solution?
arrange(key, value) %>%
mutate(team = as_factor(team),
order = row_number())
# PLOT
acup_winners_clean %>%
ggplot(aes(value, team, color = key)) +
geom_point(size = 5) +
scale_color_manual(values = c("Champions" = "#FFCC33",
"Runners-up" = "#999999",
"Third Place" = "#CC6600"),
guide = FALSE) +
labs(x = "Number of Occurrence",
title = "Winners & Losers of the Asian Cup!",
subtitle = glue("
Ordered by number of Asian Cup(s) won.
Four-time Champions, Japan, only won their first in 1992!"),
caption = glue("
Note: Israel was expelled by the AFC in 1974 while Australia joined the AFC in 2006.
Source: Wikipedia
By @R_by_Ryo")) +
facet_wrap(~key) +
theme_minimal() +
theme(text = element_text(family = "Roboto Condensed"),
title = element_text(size = 18),
plot.subtitle = element_text(size = 12),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 12),
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 12),
plot.caption = element_text(hjust = 0, size = 10),
panel.border = element_rect(fill = NA, colour = "grey20"),
panel.grid.minor.x = element_blank(),
strip.text = element_text(size = 16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment