Last active
November 5, 2021 01:24
-
-
Save PaulC91/65ae927f71f91805920b7a6f09c46c04 to your computer and use it in GitHub Desktop.
R code to produce an animated map of FIFA World Cup Winners/Runners-up using the new gganimate package
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(rvest) | |
library(opencage) # need to get opencage API key and save as env variable below https://geocoder.opencagedata.com/pricing | |
library(sf) | |
library(rnaturalearth) | |
library(gganimate) # devtools::install_github('thomasp85/gganimate') | |
library(hrbrthemes) # devtools::install_github('hrbrmstr/hrbrthemes') | |
Sys.setenv(OPENCAGE_KEY = "xxxxxxxxxxxxxxxx") | |
url <- "https://en.wikipedia.org/wiki/List_of_FIFA_World_Cup_finals" | |
tables <- read_html(url) %>% html_table() | |
wc_table <- tables[[3]] %>% | |
slice(1:21) %>% | |
select(1, 2, 4, 6) %>% | |
mutate(Year = str_sub(Year, 1, 4)) %>% | |
mutate_at(vars(2:3), funs(recode(., "Czechoslovakia" = "Czechia", "West Germany" = "Germany"))) | |
locations <- wc_table$Location %>% | |
map_df(~ { | |
geoc <- opencage_forward(placename = .x) | |
lat <- geoc$results$geometry.lat[1] | |
long <- geoc$results$geometry.lng[1] | |
tibble(Location = .x, lat = lat, lon = long) | |
}) %>% | |
bind_cols(wc_table[,1:3]) %>% | |
gather(wl, country, 5:6) %>% | |
mutate(wl = factor(wl, levels = c("Winners", "Runners-up"))) | |
world <- rnaturalearth::ne_download(category = "cultural", returnclass = "sf", type = "map_units") %>% | |
st_transform(crs = 4326) %>% | |
filter(!NAME %in% c("Fr. S. Antarctic Lands", "Antarctica")) | |
wc_geo <- inner_join(world, locations, by = c("NAME" = "country")) %>% | |
select(Country = NAME, 95:100) %>% | |
arrange(Year) | |
p <- ggplot() + | |
geom_sf(data = world, colour = "#ffffff20", fill = "#2d2d2d60", size = .5) + | |
geom_sf(data = wc_geo, aes(fill = wl), colour = "white") + | |
geom_point(data = locations, aes(lon, lat), colour = "white") + | |
geom_text(data = locations, aes(lon, lat, label = Location), colour = "white", nudge_y = -5) + | |
coord_sf(datum = NA) + | |
scale_fill_manual(values = c("#D9A441", "#A8A8A8"), name = NULL, labels = c("Winner", "Runner-Up")) + | |
transition_states(Year, 1, 1) + | |
labs(title = "FIFA World Cup Winners, Runners Up & Final Locations", | |
subtitle = "{closest_state}", x = NULL, y = NULL, | |
caption = "@paulcampbell91 | Source: Wikipedia") + | |
theme_ft_rc(base_size = 16, caption_size = 18) + | |
theme(legend.position = c(0.9, 1.01), legend.direction = "horizontal", axis.text=element_blank()) | |
pa <- animate(p, nframes = 200, fps = 10, width = 1000, height = 500) | |
save_animation(pa, file = "wc_ani.gif") |
Author
PaulC91
commented
Nov 14, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment