R Programming Codes
START HERE
Loading R Packages
# Clean up everything
rm(list=ls())
# Set working directory
setwd("F:/Maps")
# Max number of rows to be printed on R Console screen
options(max.print=1000000)
# Checking r-version
print(version$version.string)
# Loading required packages
library("glue");library("sf");library("scales");library("htmltools");
library("leaflet")
library("htmlwidgets");library("webshot");library("tidyverse");
library("tabulizer");
library("DiagrammeR")
Creating a data management outline
fig <- DiagrammeR::grViz("
digraph boxes_and_circles {
# a 'graph' statement
graph [overlap = true, fontsize = 18]
# several 'node' statements
node [shape = box,
fixedsize = true,
style = filled,
width = 3,
height = 1,
fontname = Helvetica,
fillcolor = white]
DAT [label = 'Data have been used \nfor mapping', fontcolor = blue,
width=3, fontsize = 20, penwidth = 3, peripheries = 2]
ADM [label = 'BGD Administrative \nData', fontcolor = orange,
fontsize = 20, penwidth = 3, peripheries = 1]
COV [label = 'COVID-19 Data', fontcolor = magenta, fontsize = 20,
penwidth = 3, peripheries = 1]
VAC [label = 'COVID-19 Vaccination \nData', fontcolor = red,
fontsize = 20, penwidth = 3, peripheries = 1]
LET [label = 'Map Data', fontcolor = green, fontsize = 20,
penwidth = 3, peripheries = 1]
DF1 [label = 'Bangladesh Admin \nLevel-1 data: \nDivision,
Longitude, \nLatitude, Polygons of \ndivisions', fontcolor = black,
fontsize = 20, width = 3, height=2, penwidth = 3, peripheries = 1]
DF2 [label = 'Covid-19: \n Division, Cases, \nDeaths, Case Fatality
\nRatio or CFR', fontcolor = black, fontsize = 20, width = 3, height=2,
penwidth = 3, peripheries = 1]
DF3 [label = 'COVID-19 Vaccination:\n Division, Population taken 1st
\ndose,
% of Population taken \n1st dose, Population taken 2nd \ndose,
% of population taken \n2nd dose', fontcolor = black, fontsize = 18,
width = 3.7, height=2, penwidth = 3, peripheries = 1]
DF4 [label = 'Spatial Data: \n Open street map, \nLeaflet map, Toner
Lite \nMap, World Imagery Map', fontcolor = black, fontsize = 20,
width = 3.5, height=2, penwidth = 3, peripheries = 1]
MED [label = 'Merging Dataframes: \n Above four dataframes have been
merged by using a unique identifier (e.g., Division). \nThen, relevant
attributes data have been layerd on Leaflet Map', fontcolor = black,
fontsize = 20, width = 10.7, height=1.5, penwidth = 3, peripheries = 1]
DD [label = 'Final Map', fontcolor=blue, shape=circle, fontsize=25,
penwidth = 4, peripheries = 1]
# several 'edge' statements
DAT->ADM; DAT->COV; DAT->VAC; DAT->LET; ADM->DF1; COV->DF2;
VAC->DF3; LET->DF4;
DF1->MED;DF2->MED; DF3->MED; DF4->DD; {rank=same; MED->DD; }
}
")
# 
# {out.width=100%}
DATA MANAGEMENT
1. Bangladesh (BGD) Administrative Data
#
# I have to follow these following steps to download and access to
# Bangladesh Administrative Data for the first time.
#
# ***Step-1:*** Downloading a zipped file with this line of code.
#
# zip.url <- paste0("https://data.humdata.org/dataset/401d3fae-4262-48c9-
# 891f-461fd776d49b
# /", "resource/08736818-ae72-44a9-abd6-a51915c24921/download/bgd_adm_bbs_
# 20201113_shp.zip")
#
# ***Step-2:*** Getting working directory in your computer with this
# line of code.
#
# dir <- getwd()
#
# ***Step-3:*** Call a name of zipped file object with this line of code.
#
# zip.file <- "file_name.zip"
#
# ***Step-4:*** Combining zipped file with this line of code.
#
# zip.combine <- as.character(paste(dir, zip.file, sep = "/"))
#
# ***Step-5:*** Downloading zipped file with this line of combination of
# codes.
#
# download.file(zip.url, destfile = zip.combine)
#
# ***Step-6:*** Unzipping file in your local directory with this line
# of code.
#
# unzip(zip.file)
#
# ***Finally,***I will see this file in the unzipped folder named
# "bgd_adm_bbs_20201113_SHP" in the local directory.
# I will load/import this file in a minute.
# Load polygon data of Bangladesh Division level (layer#1)
bd_geo <- sf::st_read(paste0("F:/Maps/Data/bgd_adm_bbs_20201113_SHP/",
"bgd_admbnda_adm1_bbs_20201113.shp"),
stringsAsFactors = FALSE)
# Rename columns
names(bd_geo)[3] <- "Division"
bd_geo$Division[1] <- "Barisal"
bd_geo$Division[2] <- "Chattogram"
# Extract Coordinates from each polygons**
bd_division_points<- sf::st_point_on_surface(bd_geo)
bd_division_coords <-sf::st_coordinates(bd_division_points)
colnames(bd_division_coords) <-c("Longitude", "Latitude")
bd_division_df <- cbind(bd_geo, bd_division_coords)
# Polygon data
bd_map_data <- st_transform(bd_division_df, "+proj=longlat +datum=WGS84")
# Creating a vector with color for 8 divisions in Bangladesh
bd_map_data$col <- c("red", "blue", "yellow", "purple", "pink", "black",
"crimson", "brown")
2. COVID-19 Data: Cases, Deaths, CFR
out <- as.data.frame(extract_tables(paste0("https://cdn.who.int/media/docs/
default-source","/searo/bangladesh/covid-19-who-bangladesh-situation-reports/",
"who_covid-19-update_68_20210614.pdf?sfvrsn=4439a1fa_7"),
page = 4, method = 'stream'), stringsAsFactors = FALSE)
# Data Cleaning/Wrangling
# Remove first three lines
out <- out[-(1:3),]
# Selecting only required variables
out <- dplyr::select(out, X1, X2)
# Split column X2 into 4 by splitting by whitespace
out <- tidyr::separate(data = out, col = 2, into = c('X2.1','X2.2','X2.3',
'X2.4', 'X2.5', 'X2.6'),
sep = ' ', remove = TRUE)
# Unite two columns
df <- tidyr::unite(data = out, "X212", X2.1:X2.2, sep = "",
remove = TRUE, na.rm = TRUE)
df <- tidyr::unite(data = df, "X245", X2.4:X2.5, sep = "",
remove = TRUE, na.rm = TRUE)
df <- tidyr::unite(data = df, "X2456", X245:X2.6, sep = "",
remove = TRUE, na.rm = TRUE)
df <- tidyr::separate(data = df, X2456, c("X2456.1","X2456.2"),
sep='\\.',remove=T)
df <- tidyr::separate(data = df, X2456.1, c("X2456.11","X2456.12"),
sep = -1,remove=T)
df <- tidyr::separate(data = df, X2456.2, c("X2456.21","X2456.22"),
sep="%",remove=T)
df <- dplyr::select(df, c(1, 2, 4,5,6))
df <- tidyr::unite(data = df, X24561221, c("X2456.12", "X2456.21"),
sep = "", remove = TRUE, na.rm = TRUE)
# Naming columns
colnames(df) <- c("Division", "Cases", "Death", "CFR")
df$Cases <- as.numeric(df$Cases)
df$Death <- as.numeric(df$Death)
df$CFR <- (as.numeric(df$CFR))/10
write.csv(df, "F:/Maps/covid19_division_17June.csv")
3. COVID-19 Data: Vaccination
Screenshot of the Page-9 of the PDF document in Viewer tab in RStudio

# Cleaning Vaccination Data
location <- read.csv("F:/Maps/location_17June.csv")
# To remove first column (X)
location$X = NULL
# Split column X2 into 4 by splitting by whitespace
vac_df <- tidyr::separate(data = location, col = 2, into =
c('X2.1','X2.2','X2.3'),
sep = ' ', remove = TRUE)
vac_df <- tidyr::separate(data = vac_df, col = 5, into =
c('X3.1','X3.2'),
sep = '%', remove = TRUE)
vac_df <- tidyr::separate(data = vac_df, col = 7, into =
c('X4.1','X4.2', 'X4.3'),
sep = ' ', remove = TRUE)
vac_df <- tidyr::separate(data = vac_df, col = 10, into =
c('X5.1','X5.2'),
sep = '%', remove = TRUE)
# I have to remove commas from data frame columns in R by using gsub()
vac_df$X4.1 = as.numeric(gsub(",", "", vac_df$X4.1))
# Unite multiple columns
vac_df <- tidyr::unite(data = vac_df, "X2123", X2.1:X2.3, sep = "",
remove = TRUE,
na.rm = TRUE)
vac_df <- tidyr::unite(data = vac_df, "X4123", X4.1:X4.3, sep = "",
remove = TRUE,
na.rm = TRUE)
# Selecting desired columns
vac_df <- dplyr::select(vac_df, c(1, 2, 3, 5,6))
# (Re)naming columns
colnames(vac_df) <- c("Division", "Admin_dose_1", "Prop_popul_dose_1",
"Admin_dose_2", "Prop_popul_dose_2")
# Change Total into National
vac_df$Division[9] <- "National"
# Merging df and vac_df
case_vac_df <- dplyr::inner_join(x=df, y=vac_df, by ="Division")
# Saving file in local directory
write.csv(vac_df, "F:/Maps/covid19_vac_division_17June.csv")
# Merging vaccine data with points/polygon data
data_df <- dplyr::full_join(x= bd_map_data, y=case_vac_df, by= "Division")
# Converting character columns into numeric
data_df$Cases <- as.numeric(data_df$Cases)
data_df$Death <- as.numeric(data_df$Death)
data_df$CFR <- as.numeric(data_df$CFR)
data_df$Admin_dose_1 <- as.numeric(data_df$Admin_dose_1)
data_df$Prop_popul_dose_1 <- as.numeric(data_df$Prop_popul_dose_1)
data_df$Admin_dose_2 <- as.numeric(data_df$Admin_dose_2)
data_df$Prop_popul_dose_2 <- as.numeric(data_df$Prop_popul_dose_2)
data_cov <- dplyr::select(data_df, Division, Cases, Death, CFR,
Admin_dose_1, Prop_popul_dose_1, Admin_dose_2, Prop_popul_dose_2)
data_cov <- dplyr::rename(data_cov,
"1st dose" = Admin_dose_1,
"1st dose(%)" = Prop_popul_dose_1,
"2nd dose" = Admin_dose_2,
"2nd dose(%)" = Prop_popul_dose_2)
# Converting sf into data frame
data_cov2 <- sf::st_set_geometry(data_cov, NULL)
data_cov2$Cases <- scales::comma(data_cov2$Cases, accuracy = 1)
data_cov2$Death <- scales::comma(data_cov2$Death, accuracy = 1)
data_cov2$`1st dose` <- scales::comma(data_cov2$`1st dose`, accuracy = 1)
data_cov2$`2nd dose` <- scales::comma(data_cov2$`2nd dose`, accuracy = 1)
4. Spatial Data
data_df$Updated <- "14 June 2021"
# Generate a hyper link of data source
data_df$Source <- "<a class='weblink' href= 'https://cdn.who.int/media/docs/
default-source/searo/bangladesh/covid-19-who-bangladesh-situation-reports/
who_covid-19-update_68_20210614.pdf?sfvrsn=4439a1fa_7'
target='_blank' rel='noopener noreferrer'>WHO Bangladesh</a>"
# Creating a popup labels
covid_labels <- glue::glue(
"<strong><span style='color:blue'><span style='font-size: 14px'>
{data_df$Division} Division</strong></span><br/>
Cases: <b><span style='color:blue'>{scales::comma(data_df$Cases,
accuracy = 1)}<br/></span></b>
Death: <b><span style='color:blue'>{scales::comma(data_df$Death,
accuracy = 1)}<br/></span></b>
CFR: <b><span style='color:blue'>{scales::comma(data_df$CFR,
accuracy = 0.01)}%<br/></span></b>
Administered 1st dose: <b><span style='color:blue'>
{scales::comma(data_df$Admin_dose_1,
accuracy = 1)}<br/></span></b>
40+ pop. taken 1st dose: <b><span style='color:blue'>
{scales::comma(data_df$Prop_popul_dose_1,
accuracy = 0.1)}%<br/></span></b>
Administered 2nd dose: <b><span style='color:blue'>
{scales::comma(data_df$Admin_dose_2,
accuracy = 1)}<br/></span></b>
40+ pop. taken 2nd dose: <b><span style='color:blue'>
{scales::comma(data_df$Prop_popul_dose_2,
accuracy = 0.1)}%<br/></span></b>
Updated: <b><span style='color:blue'>
{data_df$Updated} </span><br/></b>
Source: {data_df$Source} <br/>") %>% lapply(htmltools::HTML)
bd_data <- glue::glue(
"<P><b><span style='font-size: 25px'>
<span style='color:green'>BANGLADESH<br></span><span
style='font-size:
16px'><span style='color:blue'>COVID-19 Vaccination:
</b></span><br/>Administered 1st dose: <b><span
style='color:blue'>
{scales::comma(tail(data_df$Admin_dose_1, 1),accuracy = 1)}
<br/></span></b>
% of pop. taken 1st dose: <b><span style='color:blue'>
{scales::comma(tail(data_df$Prop_popul_dose_1, 1),
accuracy = 0.1)}% <br/>
</span></b>Administered 2nd dose: <b><span style='color:blue'>
{scales::comma(tail(data_df$Admin_dose_2, 1),accuracy = 1)}<br/>
</span></b>
% of pop. taken 2nd dose: <b><span style='color:blue'>
{scales::comma(tail(data_df$Prop_popul_dose_2, 1),
accuracy = 0.1)}%
<br/></span></b>Updated: <b><span style='color:blue'>
{tail(data_df$Updated,1)}</b></span></p>")
# Making a title of the Map
tag.map.title <- tags$style(HTML("
.leaflet-control.map-title {
transform: translate(-50%,20%);
position: fixed !important;
left: 20%;
text-align: center;
padding-left: 10px;
padding-right: 10px;
background: rgba(255,255,255,0.75);
font-weight: bold;
font-size: 16px;
}
"))
title <- tags$h3(
tag.map.title, HTML("COVID-19 cases, death, CFR, and vaccination
in Bangladesh")
)
CREATING MAP
map <-leaflet::leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
addPolygons(
data = data_df,
fillColor = data_df$col,
label = NULL,
stroke = TRUE,
smoothFactor = 0.2,
fillOpacity = 0.8,
color = "black",
weight = 2) %>%
addMarkers(lng= data_df$Longitude, lat= data_df$Latitude, label =
covid_labels,
labelOptions = labelOptions(noHide = TRUE, permanent = TRUE,
direction = "right", textsize = "10px", textOnly = FALSE))%>%
addMarkers(lng= data_df$Longitude, lat= data_df$Latitude,
popup = covid_labels,
popupOptions = popupOptions(closeOnClick = FALSE,
keepInView = TRUE))%>%
addControl(title, position = "topleft", className="map-title") %>%
fitBounds(lng1 = min(data_df$Longitude, na.rm = TRUE),
lat1 = min(data_df$Latitude,na.rm = TRUE),
lng2 = max(data_df$Longitude, na.rm = TRUE),
lat2 = max(data_df$Latitude, na.rm = TRUE))%>%
addMiniMap(toggleDisplay = TRUE, tiles = providers$OpenStreetMap)%>%
addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery")%>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite")%>%
addProviderTiles(providers$OpenStreetMap, group = "Open SM")%>%
addLayersControl(baseGroups = c("Open SM", "Toner Lite", "World Imagery"),
options = layersControlOptions(collapsed = FALSE))%>%
addControl('<a rel="license" href="http://creativecommons.org/licenses
/by-sa/4.0/">
<img alt="Creative Commons License" style="border-width:0"
src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" />
</a><br />This work is licensed under a <a rel="license"
href="http://
creativecommons.org/licenses/by-sa/4.0/">Creative Commons
Attribution-ShareAlike 4.0 International License</a>.',
position = "bottomleft") %>%
addControl(bd_data, position = 'topright')
RESULTS
knitr::kable(head(data_cov2, n=50),
caption = "COVID-19: Cases, Death, CFR, Vaccine as of 13 Jun'21")
# Saving map as html file
htmlwidgets::saveWidget(widget = map,
file = "covid19_update_17June.html",
selfcontained = TRUE)
webshot::webshot("covid19_update_17June.html",
file = "bd_vaccination_june.png",
vwidth = 1200,vheight = 1200)
# 