library(tidyverse)
library(janitor)
library(countrycode)
library(ggflags)
library(ggthemes)
library(hrbrthemes)
options(scipen = 999)
35 Years of Olympic Games
<- tidytuesdayR::tt_load(2021, week = 31)
tuesdata
<- tuesdata$olympics
olympics <- tuesdata$regions regions
#glimpse(olympics)
# View(olympics)
::profile_missing(olympics) DataExplorer
glimpse(regions)
::profile_missing(regions) DataExplorer
%>%filter(is.na(region)) regions
<- regions%>%rename(noc="NOC")
regions
<- olympics %>%
my_olympics inner_join(regions,by="noc") %>%
mutate(medal=replace_na(medal,"Absent")) %>%
filter(!is.na(region)) %>%
select(-notes,-name,-team,-event) %>%
drop_na()
# DataExplorer::profile_missing(my_olympics)
::skim_to_list(my_olympics) skimr
Converting “region” vector into country code “iso2c” with {countrycodes} to be able to use {ggflags}
some values were not matched unambiguously:
- Bolivia
- Individual Olympic Athletes
- Kosovo
- Micronesia
I could do a dataset with only “Individual Olympic Athletes” and eventually use it to add some info in a geom_
<- my_olympics %>% filter(region %in% "Individual Olympic Athletes" ) %>% count(sex)
IOA
IOA
After a quick look at the “ambiguous_country_codes”, just one is relevant with a “Gold medal”, then will see how to use it.
<- c("Boliva","Kosovo","Micronesia")
ambiguous_country_codes
<- my_olympics %>% filter(!region %in% "Individual Olympic Athletes",
kos_gold %in% ambiguous_country_codes,
region =="Gold")
medal
kos_gold
Update “my_olympics” with countrycode()
:
<- my_olympics %>%
my_olympics filter(!region %in% "Individual Olympic Athletes",
!region %in% ambiguous_country_codes)
35 Years: 1896 to 2016
%>%count(year) my_olympics
Age: 11 to 71 (61 different ages)
%>% count(age) my_olympics
Section Age & Sex
<- my_olympics %>%
age_sex_plot group_by(sex) %>%
summarize(age,height,weight,year) %>% # count(age)
ungroup() %>%
ggplot(aes(x=factor(age),fill=factor(sex))) +
geom_bar(position="stack") +
scale_fill_fivethirtyeight() +
labs(title="Distribution of age by sex",
subtitle="61 different age years form 11 to 71 years old gamers",
fill="Sex") +
theme_fivethirtyeight() +
theme(axis.text.x = element_text(angle=20),
legend.position = "bottom",
plot.title = element_text(size=16,vjust=-0.5),
panel.grid.major.x = element_blank())
age_sex_plot
Composition of plots and background
library(magick)
library(ggimage)
library(ggpubr)
library(cowplot)
library(extrafont)
library(showtext)
fonts()
<-"Roboto Condensed"
olympics_family#library(ggflags)
library(countrycode)
<- ggplot() +
olympics_plot geom_blank() +
theme_void() +
theme()
<-"olympics.png"
img_olympics
<- ggbackground(age_sex_plot, img_olympics,alpha=.4, color="#CD919E")
plot
+
plot theme(plot.background = element_rect())#fill="#FFEFDB"))
Then add a secon plot on the right corner
Section medals
“At the 1968 Summer Olympics in Mexico City, 29 events in swimming were contested. There was a total of 468 participants from 51 countries competing. The United States dominated the competition, winning 52 of 87 possible medals. 15-year-old phenom Debbie Meyer from Maryland won three gold medals.”
source: Swimming at the 1968 Summer Olympics
<- my_olympics%>%
my_olympics_with_country_codesmutate(country_code = countrycode(region,
origin = 'country.name',
destination = 'iso2c'),
country_code=tolower(country_code)) %>%
select(year,sport,medal,country_code) %>%
filter(medal=="Gold") %>%
count(year,sport,country_code) %>%
arrange(year)
my_olympics_with_country_codes
<- c("Athletics","Wrestling","Swimming","Shooting","Rowing",
my_favourites "Boxing","Canoeing","Cycling")
<- my_olympics_with_country_codes%>%
my_favourite_sports filter(sport %in%my_favourites)
library(ggflags)
<- ggplot(data=my_favourite_sports,
top_golden_sports aes(x=factor(year),y=fct_reorder(sport,-year))) +
geom_point(shape = 21, colour = "gold", fill = NA, size = 7, stroke = 1) +
::geom_flag(aes(country=country_code), size=4.5) +
ggflags::scale_country() +
ggflagsguides(country="none") +
labs(title="Top 8 Gold medal sport winners",
x="",y="") +
theme_fivethirtyeight()+
theme(axis.text.x = element_text(angle=90),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_line(color="#CD919E"),
plot.title.position = "plot",
plot.background = element_blank(),
panel.background = element_blank())
# top_golden_sports
#png("top_golden_sports.png")
ggsave("top_golden_sports.png", width = 8, height = 6)
print(top_golden_sports)
dev.off()
library(cowplot)
<- "top_golden_sports.png"
img
<- ggdraw() +
inset.plot draw_image(img, x = 0.22, y = -0.2, scale = .70)
<- inset.plot + theme(legend.position = "none",
inset.plot plot.background = element_blank(),
panel.background = element_blank())
<- age_sex_plot + theme(legend.position = "none",
inset.plot2 plot.background = element_blank(),
panel.background = element_blank())
<- ggdraw() +
double_plot draw_plot(inset.plot2, x = 0, y = 0, width = 1, height = 1) +
draw_plot(inset.plot, x = -0.35, y = .18, width = 1.5, height = 0.9)
double_plot
<-"olympics.png"
img_olympics<- ggbackground(double_plot, img_olympics,alpha=.2, color="#CD919E")
plot
+
plot theme(plot.background = element_rect(fill="#FFEFDB"))
<- ggarrange(plot)
graphics
<- annotate_figure(graphics,
annotation_plot top = text_grob("Olympics outlook 1896 - 2016 ",color =c("#FF4040", "#FFFFFF", "#FFFFFF"),
face = "bold", size = 40,family=olympics_family),
bottom = text_grob("DataViz: @fgazzelloni DataSource: \n TidyTuesday week31, Olympic Medals, Kaggle, Financial Times & FiveThirtyEight",
color = c("black"),family=olympics_family,
hjust = 0.5, x = 0.5, face = "bold.italic", size = 10),
left = text_grob("", color = c("#778899"), rot = 90,size=1),
right = text_grob(bquote(""), color=c("#778899"),rot = 90,size=1),
fig.lab = "TidyTuesday week31\n", fig.lab.face = "bold.italic",fig.lab.size=7,
fig.lab.pos="bottom.right"
)
<- annotation_plot +
final_plot annotate(geom = "text", label="The historical dataset on the modern Olympic Games,\n including all the Games from Athens 1896 to Rio 2016",x = 0.7, y = 0.81,
colour = "black", size = 6,family=olympics_family)
final_plot
Attach the Olympic logo at the sides of the title
<-"olympics.png"
img_olympics
<- ggdraw() +
final draw_image(img_olympics, x = -0.35, y = 0.45, scale = .10) +
draw_image(img_olympics, x = 0.32, y = 0.45, scale = .10) +
draw_plot(final_plot)
Saving:
::agg_png(here::here("w31", "w31_olympics.png"),
raggres = 320, width = 14, height = 8, units = "in")
final
dev.off()