Load packages

The following R packages are used for this analysis:

  1. readxl
  2. tidyverse
  3. sf
  4. xml2
  5. rvest
  6. RCurl
  7. stringr
  8. leaflet

Web scrapping and Download files

# Link of the page
url <- "https://covid19.who.int/data"

# Create an html document of the page
webpage <- xml2::read_html(url)

# Extract the urls on the page
urls <- webpage %>% 
  rvest::html_nodes("a") %>% 
  rvest::html_attr("href") 

# Extract the required url
(myurl <- urls[str_detect(urls, "WHO-COVID-19-global-table-data.csv")]) # Or myurl <- urls[42]
[1] NA
# filename
data_file <- "WHO-COVID-19-global-table-data.csv"

# Download file
if(!file.exists(data_file)){
  download.file(myurl, data_file)
}

Load file

# Read files into R
covid_file <- "WHO-COVID-19-global-table-data.csv"
covid <- read_csv(covid_file, col_type = cols())
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
b_world <- read_sf("world-administrative-boundaries.shp")

Data exploration

# Remove the first row of covid
new_covid <- subset(covid, Name != "Global") # Or new_covid <- covid[-1,]

# Check the difference in the name variables of the files
setdiff(b_world$name, new_covid$Name)
 [1] "Cape Verde"                                
 [2] "The former Yugoslav Republic of Macedonia" 
 [3] "Bouvet Island"                             
 [4] "Pitcairn Island"                           
 [5] "French Southern and Antarctic Territories" 
 [6] "British Indian Ocean Territory"            
 [7] "Guantanamo"                                
 [8] "Glorioso Islands"                          
 [9] "Paracel Islands"                           
[10] "Abyei"                                     
[11] "Gaza Strip"                                
[12] "Bosnia & Herzegovina"                      
[13] "Reunion"                                   
[14] "Midway Is."                                
[15] "Taiwan"                                    
[16] "Azores Islands"                            
[17] "Vietnam"                                   
[18] "Jarvis Island"                             
[19] "Svalbard and Jan Mayen Islands"            
[20] "Aksai Chin"                                
[21] "Norfolk Island"                            
[22] "Moldova, Republic of"                      
[23] "Hala'ib Triangle"                          
[24] "Cocos (Keeling) Islands"                   
[25] "Czech Republic"                            
[26] "Swaziland"                                 
[27] "Ma'tan al-Sarra"                           
[28] "Western Sahara"                            
[29] "Hong Kong"                                 
[30] "Netherlands Antilles"                      
[31] "Northern Mariana Islands"                  
[32] "Kuril Islands"                             
[33] "U.K. of Great Britain and Northern Ireland"
[34] "Ilemi Triangle"                            
[35] "Arunachal Pradesh"                         
[36] "West Bank"                                 
[37] "Spratly Islands"                           
[38] "Turkey"                                    
[39] "Christmas Island"                          
[40] "Macao"                                     
[41] "Heard Island and McDonald Islands"         
[42] "Jammu-Kashmir"                             
[43] "South Georgia & the South Sandwich Islands"
[44] "Venezuela"                                 
[45] "Côte d'Ivoire"                             
[46] "Antigua & Barbuda"                         
[47] "Libyan Arab Jamahiriya"                    
[48] "Bolivia"                                   
[49] "Madeira Islands"                           
# Rename country names for easier join
cnames <- c(
"The former Yugoslav Republic of Macedonia"="North Macedonia",
"Pitcairn Island"="Pitcairn Islands",
"Bosnia & Herzegovina"="Bosnia and Herzegovina",
"Vietnam"="Viet Nam",
"Moldova, Republic of"="Republic of Moldova",
"Sao Tome and Principe"="Sao Tome and Principe",
"Czech Republic"="Czechia",
"Swaziland"="Eswatini",
"Northern Mariana Islands"="Northern Mariana Islands (Commonwealth of the",
"U.K. of Great Britain and Northern Ireland"="The United Kingdom",
"Turkey"="Turkmenistan",
"Venezuela"="Venezuela (Bolivarian Republic of",
"Côte d'Ivoire"="Côte d’Ivoire",
"Antigua & Barbuda"="Antigua and Barbuda"
)

# Rename observations
b_world$name <- b_world$name %>% str_replace_all(cnames)

# Merge the data files
covid_world <- b_world %>% left_join(new_covid, c("name" = "Name")) %>% drop_na()

#write_csv(covid_world, "covid_world.csv")

Cleanse and format

# Format the name of columns by removing spaces
new_names = c("status", "color_code", "region", "iso3", "continent", "name", "iso3166", "french_shor", "geometry", "WHO_Region", "Cases_cum_total", "Cases_cum_total_per_100000", "New_Cases_7_days", "New_Cases_7_days_per_100000", "New_Cases_24_hours", "Deaths_cum_total", "Deaths_cum_total_per_100000", "New_Deaths_7_days", "New_Deaths_7_days_per_100000", "New_Deaths_24_hours")

# Set new name for columns
covid_world <- covid_world %>% set_names(new_names)

# Create labels for the map
labels <- sprintf(
  "<strong>%s</strong><br/>%g Cumulative cases</br>
  %g Cumulative deaths",
  covid_world$name, 
  covid_world$Cases_cum_total, covid_world$Deaths_cum_total
) %>% lapply(htmltools::HTML)

Map creation

# Global map using leaflet
map <- leaflet(covid_world) %>% 
  setView(-9.081999, 8.675277, 4) %>% 
  addTiles(providers$OpenTopoMap) %>% 
  addPolygons(data = covid_world$geometry, 
              fillColor = topo.colors(10, alpha = NULL), 
              weight = 2, opacity = 1, 
              color = "white", dashArray = "3",
              fillOpacity = 0.7, highlightOptions = highlightOptions(
                weight = 5,
                color = "#222",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE ),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto"))
map
ggsave("map.svg")
Saving 7 x 5 in image
ggsave("map.png")
Saving 7 x 5 in image
ggsave("map.jpg")
Saving 7 x 5 in image
ggsave("map.pdf")
Saving 7 x 5 in image