Show percentage of gold, silver, bronze and total medals received
# Call packages ----
library(rvest)
library(ggplot2)
library(dplyr)
library(patchwork)
# Get data ----
url <- "https://olympics.com/tokyo-2020/olympic-games/en/results/all-sports/medal-standings.htm?utm_campaign=dp_msn"
ds <- read_html(url)
ds <- html_table(html_nodes(ds, "table")[[1]])
names(ds)
## [1] "Rank" "Team/NOC" "" "" ""
## [6] "Total" "RankbyTotal" "NOCCode"
names(ds) <- c("Rank","Team","Gold","Silver", "Bronze", "Total" , "RankbyTotal", "NOCCode")
# Define colours ----
goldColour <- "#FFD700"
silverColour <- "#C0C0C0"
bronzeColour <- "#CD7F32"
# Show Gold Medal chart ----
goldMedals <- ds%>%
dplyr::select(Team, Gold)%>%
dplyr::arrange(- Gold)%>%
head(10)
pl <- ggplot(data = goldMedals, aes(x = reorder(Team, Gold), y = Gold))
pl <- pl + geom_bar(stat ="identity", fill = goldColour)
pl <- pl + geom_text(aes(label = Gold), hjust = -0.2, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Gold medals")
pl <- pl + labs(title ="Top 10 countries by Gold medals")
pl

pl1 <- pl
# Show Silver Medal chart ----
silverMedals <- ds%>%
dplyr::select(Team, Silver)%>%
dplyr::arrange(- Silver)%>%
head(10)
pl <- ggplot(data = silverMedals, aes(x = reorder(Team, Silver), y = Silver))
pl <- pl + geom_bar(stat ="identity", fill = silverColour)
pl <- pl + geom_text(aes(label = Silver), hjust = -0.2, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Silver medals")
pl <- pl + labs(title ="Top 10 countries by Silver medals")
pl

pl2 <- pl
# Show Bronze Medal chart ----
bronzeMedals <- ds%>%
dplyr::select(Team, Bronze)%>%
dplyr::arrange(- Bronze)%>%
head(10)
pl <- ggplot(data = bronzeMedals, aes(x = reorder(Team, Bronze), y = Bronze))
pl <- pl + geom_bar(stat ="identity", fill = bronzeColour)
pl <- pl + geom_text(aes(label = Bronze), hjust = -0.2, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Bronze medals")
pl <- pl + labs(title ="Top 10 countries by Bronze medals")
pl

pl3 <- pl
# Show Total Medal chart ----
totalMedals <- ds%>%
dplyr::select(Team, Total)%>%
dplyr::arrange(- Total)%>%
head(10)
pl <- ggplot(data = totalMedals, aes(x = reorder(Team, Total), y = Total))
pl <- pl + geom_bar(stat ="identity", fill = "Blue")
pl <- pl + geom_text(aes(label = Total), hjust = -0.2, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Overall medals")
pl <- pl + labs(title ="Top 10 countries by Total medals")
pl

pl4 <- pl
(pl1 + pl2) / (pl3+ pl4)

# Percentage of Gold medals ----
goldPerc <- ds%>%
dplyr::mutate(perc = round(Gold/sum(Gold)*100,1))%>%
dplyr::arrange(- Gold)%>%
dplyr::select(Team, Gold, perc)%>%
head(10)
pl <- ggplot(data = goldPerc, aes(x = reorder(Team, perc), y = perc))
pl <- pl + geom_bar(stat ="identity", fill = goldColour)
pl <- pl + geom_text(aes(label = paste0(perc, "%")), hjust = -0.1, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Percentage share")
pl <- pl + labs(title ="Top 10 teams based on perc share of gold medals")
pl

pl5 <- pl
# Percentage of Silver medals ----
silverPerc <- ds%>%
dplyr::mutate(perc = round(Silver/sum(Silver)*100,1))%>%
dplyr::arrange(- Silver)%>%
dplyr::select(Team, Silver, perc)%>%
head(10)
pl <- ggplot(data = silverPerc, aes(x = reorder(Team, perc), y = perc))
pl <- pl + geom_bar(stat ="identity", fill = silverColour)
pl <- pl + geom_text(aes(label = paste0(perc, "%")), hjust = -0.1, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Percentage share")
pl <- pl + labs(title ="Top 10 teams based on perc share of silver medals")
pl

pl6 <- pl
# Percentage of Bronze medals ----
bronzePerc <- ds%>%
dplyr::mutate(perc = round(Bronze/sum(Bronze)*100,1))%>%
dplyr::arrange(- Bronze)%>%
dplyr::select(Team, Bronze, perc)%>%
head(10)
pl <- ggplot(data = silverPerc, aes(x = reorder(Team, perc), y = perc))
pl <- pl + geom_bar(stat ="identity", fill = bronzeColour)
pl <- pl + geom_text(aes(label = paste0(perc, "%")), hjust = -0.1, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Percentage share")
pl <- pl + labs(title ="Top 10 teams based on perc share of bronze medals")
pl

pl7 <- pl
# Percentage of Total medals ----
totalPerc <- ds%>%
dplyr::mutate(perc = round(Total/sum(Total)*100,1))%>%
dplyr::arrange(- Total)%>%
dplyr::select(Team, Total, perc)%>%
head(10)
pl <- ggplot(data = totalPerc, aes(x = reorder(Team, perc), y = perc))
pl <- pl + geom_bar(stat ="identity", fill = "Blue")
pl <- pl + geom_text(aes(label = paste0(perc, "%")), hjust = -0.1, size = 2.5)
pl <- pl + theme_minimal() + theme(panel.grid = element_blank())
pl <- pl + coord_flip()
pl <- pl + labs(x= "",y = "Percentage share")
pl <- pl + labs(title ="Top 10 teams based on perc share of total medals")
pl

pl8 <- pl
(pl5 + pl6) / (pl7+ pl8)
