Last active
April 10, 2019 17:21
-
-
Save gonzalezgouveia/0bfe6045e5884b8a88b077106e261420 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(ggthemes) | |
# Por Rafa @GonzalezGouvia | |
# leyendo directo de la página | |
partidos_fifa_copa_mundial_procesado <- readr::read_delim("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-04-10/partidos.txt", delim = "\t") | |
# función auxiliar para extraer ultimo partido | |
quita_parentesis <- function(texto){ | |
as.integer(gsub("\\((.*)\\)", "\\1", texto)) | |
} | |
# reorganización de los datos | |
partidos_tidy <- partidos_fifa_copa_mundial_procesado %>% | |
# dejando solo partidos de finales | |
mutate(partido_orden_int = quita_parentesis(partido_orden)) %>% | |
group_by(anio) %>% | |
mutate(last = last(partido_orden_int)) %>% | |
filter(last == partido_orden_int) %>% | |
# uniendo columnas | |
unite("equipo_1_goles", c(equipo_1, equipo_1_final)) %>% | |
unite("equipo_2_goles", c(equipo_2, equipo_2_final)) %>% | |
gather("equipo_n", "pais_goles", c(equipo_1_goles, equipo_2_goles)) %>% | |
separate(pais_goles, c("pais","goles"), sep = '_') %>% | |
mutate(goles = as.integer(goles)) %>% | |
arrange(fecha, partido_orden_int) | |
# uniendo las alemanias :D | |
partidos_tidy$pais <- gsub('Alemania occidental', 'Alemania', partidos_tidy$pais) | |
# agrupando paises | |
cuantas_finales <- partidos_tidy %>% | |
group_by(pais) %>% | |
summarise(n = n()) %>% | |
mutate(pais = fct_reorder(pais, n, .desc = FALSE)) | |
# graficando | |
cuantas_finales %>% | |
ggplot(aes(x = pais, | |
y = n, | |
fill = pais)) + | |
geom_col() + | |
coord_flip() + | |
# temas de ggthemes | |
theme_wsj() + scale_color_stata() + scale_fill_stata() + | |
labs(title = 'Participación en finales') + | |
# ajustando algunos aspectos de theme_wsj | |
theme(legend.position = "none", | |
panel.grid.major.x = element_line(colour = 'black'), | |
panel.grid.major.y = element_blank(), | |
title = element_text(family = "mono", size = rel(1.3))) | |
# Gracias por mirar este código hasta el final! :D | |
# Por Rafa @GonzalezGouvia |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment