2 Cost of hosting world cup
Let’s start by loading the libraries that we’ll utilize in our analysis
#web scrapping
library(rvest)
#everything tidy?
library(tidyverse)
#handling spatial-data
library(rnaturalearth)
library(rnaturalearthdata)
library(sf)
library(ggflags)
library(ggspatial)
library(giscoR)
library(rasterpic)
library(countrycode)
#adding flags in ggplot
library(ggimage)
#Visually explore data tables
library(visdat)
#fit text within a defined area
library(ggfittext)
#set the default ggplot theme
theme_set(cowplot::theme_cowplot())
#get image
<- here::here("images/wc12.png")
wc_img #define function to control transparency
.1 <- function(img) {
transparent_0::image_fx(img, expression = glue::glue("0.1*a"), channel = "alpha")
magick
}.2 <- function(img) {
transparent_0::image_fx(img, expression = glue::glue("0.2*a"), channel = "alpha")
magick
}# get flags form this repo
<- "https://raw.githubusercontent.com/hjnilsson/country-flags/master/png250px/" flagrepo
#
# Extract the amount of money
<- "https://en.wikipedia.org/wiki/Economics_of_the_FIFA_World_Cup"
url_2
<- url_2 %>%
tbls_lst_2 %>%
read_html html_table()
#
<- tbls_lst_2[[2]] %>%
df_4 ::clean_names() %>%
janitormutate(host = str_replace(host, "/\\s", "-")) %>%
extract(host, c("country","year"), "([A-Z-]+) (\\([0-9]+\\))") %>%
separate_rows(country,sep = "-") %>%
mutate(year = parse_number(year),
#x = strex::str_first_currency(general_cost)[[4]],
x = parse_number(general_cost),
y = ifelse(str_detect(general_cost, "million"),x/1000,x))
$iso2 <- countrycode(df_4$country, "ioc", "iso2c") df_4
ggplot()+
geom_col(data = df_4 %>% distinct(year, y),
aes(year, y),
fill = "grey90", color = "black", size = 1)+
geom_text(data = df_4 %>% distinct(year, y),
aes(year, y, label = y),
size = 5, nudge_y = 8)+
scale_y_continuous(breaks = c(1,10,100,200))+
labs(y = "Billion $")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
ggplot()+
geom_col(data = df_4 %>% distinct(year, y),
aes(year, y))+
geom_text(data = df_4 %>% distinct(year, y) %>% mutate(y2 =y, y = glue::glue("${y} bn")),
aes(year, y2, label = y),
size = 4, nudge_y = 20)+
::geom_flag(data = df_4 %>%
ggimagefilter(!country %in% c("JPN", "KOR")),
aes(year, y+8, image = iso2),
size = 0.07)+
::geom_flag(data = df_4 %>%
ggimagefilter(country %in% c("JPN", "KOR")) %>%
mutate(year=ifelse(str_detect(country, "JPN"), year+1.5, year)),
aes(year-0.7, y+8, image = iso2),
size = 0.05)+
::geom_col_pattern(data = df_4 %>% distinct(year, y),
ggpatternaes(year, y),
pattern_filename = I("images/Gold-Dollar-Sign-PNG-HD.png"),
pattern = 'image',
pattern_type = 'tile',
pattern_scale = -1,
fill = "#a1d99b"
+
)::geom_image(data = data.frame(x = 1995, y = 120),
ggimageaes(x,y),
image = wc_img,image_fun = transparent_0.2,
size = 1.2)+
scale_x_continuous(breaks = unique(df_4$year))+
scale_y_continuous(breaks = c(1,10,100,200))+
labs(y = "", x = "")+
theme(plot.background = element_rect(fill = "#e5f5e0"),
panel.background = element_rect(fill = "#e5f5e0"),
panel.grid.major = element_line(colour = "#e5f5e0"),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank())
<- c()
flags # Loop and add
for (iso in df_4$iso2) {
# Download pic and plot
<- paste0(flagrepo, tolower(iso), ".png")
imgurl <- tempfile(fileext = ".png")
tmpfile download.file(imgurl, tmpfile, quiet = TRUE, mode = "wb")
<- c(flags, tmpfile)
flags }
ggplot()+
::geom_image(data = data.frame(x = 1995, y = 30),
ggimageaes(x,y),
image = wc_img,
image_fun = transparent_0.1,
size = 1.2)+
::geom_image(data = df_4 %>%
ggimagedistinct(year, y) %>%
mutate(y2 = y+(y*0.85)) ,
aes(year, y2),
size = df_4 %>% distinct(year, y) %>% mutate(x = ceiling(log10(y*10))/10) %>% pull(x),
image = "images/Gold-Dollar-Sign-PNG-HD.png",
image_fun = transparent_0.2)+
::geom_col_pattern(data = df_4 ,
ggpatternaes(year, y, fill = country),
position = position_dodge(),
pattern_gravity = I("West"),
pattern_filename = I(flags),
pattern = 'image',
pattern_type = 'tile',
pattern_scale = -1,
show.legend = FALSE)+
geom_text(data = df_4 %>%
distinct(year, y) %>%
mutate(y2 = y+(y*0.45),
y = glue::glue("${y} bn")),
aes(year, y2, label = y),
size = 4)+
geom_text(data = df_4%>%
arrange(country) %>%
group_by(year) %>%
summarise(iso2 = paste(iso2, collapse = "-")),
aes(year, label = iso2),
y = -0.07,
size = 4)+
scale_y_continuous(breaks = c(1,10,100,200),
trans = scales::pseudo_log_trans(base = 10),
expand = expansion(add = c(0.2,0.5))
+
)scale_x_continuous(breaks = unique(df_4$year))+
labs(y = "", x = "")+
theme(plot.background = element_rect(fill = "#e5f5e0"),
panel.background = element_rect(fill = "#e5f5e0"),
panel.grid.major = element_line(colour = "#e5f5e0"),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size = 15))
ggplot()+
::geom_image(data = expand_grid(x = c(seq(1990, 2022,length.out = 6)),
ggimagey = c(1, 10, 100)),
aes(x,y),
image = "images/Gold-Dollar-Sign-PNG-HD.png",image_fun = transparent_0.2,
size = 0.3)+
::geom_col_pattern(data = df_4 ,
ggpatternaes(year, y, fill = country),
position = position_dodge(),
pattern_gravity = I("West"),
pattern_filename = I(flags),
pattern = 'image',
pattern_type = 'tile',
pattern_scale = -1,
show.legend = FALSE)+
scale_y_continuous(breaks = c(1,10,100,200),
trans = scales::pseudo_log_trans(base = 10),
expand = expansion(add = c(NA,0.5))
+
)scale_x_continuous(breaks = unique(df_4$year))+
geom_text(data = df_4 %>% distinct(year, y) %>% mutate(y2 = y+(y*0.45), y = glue::glue("${y} bn")),
aes(year, y2, label = y),
size = 4)+
geom_text(data = df_4%>%
arrange(country) %>%
group_by(year) %>%
summarise(iso2 = paste(iso2, collapse = "-")),
aes(year, label = iso2),
y = -0.07,
size = 4)+
labs(y = "", x = "")+
theme(plot.background = element_rect(fill = "#e5f5e0"),
panel.background = element_rect(fill = "#e5f5e0"),
panel.grid.major = element_line(colour = "#e5f5e0"),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank())