Our youtube channel

Our youtube channel has lots of videos on data visualisation in r.

Visit our youtube channel https://www.youtube.com/c/TechAnswers88

Grab Tokyo 2020 medal tally from website

Transform the data

Create Treemap for gold medals tally

# Call packages ----

library(rvest) 
library(ggplot2)
library(dplyr)
library(treemapify)

 
# 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")
 

# Show Gold Medal chart ----
goldMedals <- ds%>%
              dplyr::select(Rank,Team, Gold)%>%
              dplyr::arrange(- Gold)%>%
              head(10)

# Plot treemap chart
pl <- ggplot(data = goldMedals
             , aes(fill = Team, area = Gold
              ,  label = paste0("Rank", Rank, "\n",  Team,"\n", Gold)))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre", size = 9)
pl <- pl + labs(title ="Tokyo 2020 Olympics")
pl <- pl + labs(subtitle ="Final gold medal tally")
pl <- pl + labs(caption ="#techanswers88 on youtube")
pl <- pl + theme(legend.position = "none")

pl