Skip to content

Instantly share code, notes, and snippets.

@Ryo-N7
Created January 5, 2019 13:03
Show Gist options
  • Save Ryo-N7/69ff2b73f9c6a1eb49716c28019b19aa to your computer and use it in GitHub Desktop.
Save Ryo-N7/69ff2b73f9c6a1eb49716c28019b19aa to your computer and use it in GitHub Desktop.
pacman::p_load(tidyverse, scales, glue, extrafont, rvest, ggtextures, cowplot, polite)
# Roboto Condensed font (from hrbrmstrthemes or just Google it)
loadfonts()
# web scrape
topg_url <- "https://en.wikipedia.org/wiki/AFC_Asian_Cup_records_and_statistics"
session <- bow(topg_url)
ac_top_scorers <- scrape(session) %>%
html_nodes("table.wikitable:nth-child(29)") %>%
html_table() %>%
flatten_df() %>%
select(-Ref.) %>%
set_names(c("total_goals", "player", "country"))
# clean
ac_top_scorers <- ac_top_scorers %>%
head(5) %>%
mutate(image = "https://www.emoji.co.uk/files/microsoft-emojis/activity-windows10/8356-soccer-ball.png")
# plot basic
ac_top_graph <- ac_top_scorers %>%
ggplot(aes(x = reorder(player, total_goals), y = total_goals,
image = image)) +
geom_isotype_col(img_width = grid::unit(1, "native"), img_height = NULL,
ncol = NA, nrow = 1, hjust = 0, vjust = 0.5) +
coord_flip() +
scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10, 12, 14),
expand = c(0, 0),
limits = c(0, 15)) +
ggthemes::theme_solarized() +
labs(title = "Top Scorers of the Asian Cup",
subtitle = "Most goals in a single tournament: 8 (Ali Daei, 1996)",
y = "Number of Goals", x = NULL,
caption = glue("
Source: Wikipedia
By @R_by_Ryo")) +
theme(text = element_text(family = "Roboto Condensed"),
plot.title = element_text(size = 22),
plot.subtitle = element_text(size = 14),
axis.text = element_text(size = 14),
axis.title.x = element_text(size = 16),
axis.line.y = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.ticks.y = element_blank())
# plot: add y-axis flag images
axis_image <- axis_canvas(ac_top_graph, axis = 'y') +
draw_image("https://upload.wikimedia.org/wikipedia/commons/c/ca/Flag_of_Iran.svg",
y = 13, scale = 1.5) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/0/09/Flag_of_South_Korea.svg",
y = 10, scale = 1.7) +
draw_image("https://upload.wikimedia.org/wikipedia/en/9/9e/Flag_of_Japan.svg",
y = 7, scale = 1.7) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/f/f6/Flag_of_Iraq.svg",
y = 4, scale = 1.6) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/a/aa/Flag_of_Kuwait.svg",
y = 1, scale = 1.2)
ggdraw(insert_yaxis_grob(ac_top_graph, axis_image, position = "left"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment