Skip to content

Instantly share code, notes, and snippets.

@Ryo-N7
Last active June 7, 2019 12:51
Show Gist options
  • Save Ryo-N7/6d7251db9fe3b92be16a13bd7d8deae1 to your computer and use it in GitHub Desktop.
Save Ryo-N7/6d7251db9fe3b92be16a13bd7d8deae1 to your computer and use it in GitHub Desktop.
Top scorers of the Copa America
# pkgs
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
library(rvest)
library(polite)
library(ggplot2)
library(scales)
library(cowplot)
library(glue)
library(ggtextures)
library(extrafont)
loadfonts()
# scrape
url <- "https://es.wikipedia.org/wiki/Anexo:Estad%C3%ADsticas_de_la_Copa_Am%C3%A9rica"
session <- bow(url)
copa_top_scorers <- scrape(session) %>%
html_nodes(".mw-parser-output > table:nth-child(95)") %>%
html_table() %>%
flatten_df() %>%
set_names(c("player", "country", "goals")) %>%
mutate(image = "https://www.emoji.co.uk/files/microsoft-emojis/activity-windows10/8356-soccer-ball.png")
# plot!
copa_goleadores_plot <- copa_top_scorers %>%
head(5) %>%
ggplot(aes(x = reorder(player, goals), y = goals,
image = image)) +
geom_isotype_col(img_width = grid::unit(1, "native"), img_height = NULL,
ncol = NA, nrow = 1, hjust = 0, vjust = 0.5) +
geom_text(aes(label = goals, family = "Roboto Condensed", fontface = "bold"),
size = 7.5, color = "yellow",
nudge_y = 0.5) +
coord_flip() +
scale_y_continuous(breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18),
expand = c(0, 0),
limits = c(0, 19)) +
labs(title = "Top Scorers of the Copa América",
subtitle = glue("
Most goals in a single tournament: 9
Humberto Maschio (Argentina), Javier Ambrois (Uruguay), Jair (Brazil)"),
y = "Number of Goals", x = NULL,
caption = glue("
Source: Wikipedia
By @R_by_Ryo")) +
theme(text = element_text(family = "Roboto Condensed", color = "white"),
plot.title = element_text(family = "Roboto Condensed", face = "bold",
size = 26, color = "yellow"),
plot.subtitle = element_text(size = 16),
plot.caption = element_text(size = 12),
panel.background = element_rect(fill = "#009b3a"),
plot.background = element_rect(fill = "#002776"),
axis.text = element_text(size = 18, color = "white"),
axis.title.x = element_text(size = 18),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
panel.grid.major = element_line(size = 1.1, color = "white"),
panel.grid.minor = element_line(size = 1.1, color = "white"),
#panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
#panel.grid.minor = element_blank(),
#panel.grid = element_line(size = 1.1, color = "white"),
axis.ticks = element_blank())
axis_image <- axis_canvas(copa_goleadores_plot, axis = 'y') +
draw_image("https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg",
y = 16.5, scale = 1.8) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/1/1a/Flag_of_Argentina.svg",
y = 12.5, scale = 1.8) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/f/fe/Flag_of_Uruguay.svg",
y = 9, scale = 1.8) +
draw_image("https://upload.wikimedia.org/wikipedia/commons/d/df/Flag_of_Peru_%28state%29.svg",
y = 5.25, scale = 1.8) +
draw_image("https://upload.wikimedia.org/wikipedia/en/0/05/Flag_of_Brazil.svg",
y = 1.5, scale = 1.8)
## fig.height = 8, fig.width = 10
ggdraw(insert_yaxis_grob(copa_goleadores_plot, axis_image, position = "left"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment