Load libraries
library(rvest)
## Loading required package: xml2
library(stringr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(leaflet)
library(geojsonio)
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
Pull province data from Wikipedia
webpage <- read_html("https://en.wikipedia.org/wiki/COVID-19_pandemic_in_Nepal")
covid19_nepal <- webpage %>%
html_node(xpath = '//*[@id="covid19-container"]/table') %>%
html_table(fill = TRUE)
head(covid19_nepal)
## [show all]vteCOVID-19 cases in Nepal by province and district[61]Last updated: 16 Nov 10:51 AM NPT
## 1 Location
## 2 Province No. 1
## 3 Morang
## 4 Sunsari
## 5 Jhapa
## 6 Udayapur
## [show all]vteCOVID-19 cases in Nepal by province and district[61]Last updated: 16 Nov 10:51 AM NPT
## 1 Index case
## 2 17 Apr
## 3 17 May
## 4 18 May
## 5 24 Apr
## 6 17 Apr
## [show all]vteCOVID-19 cases in Nepal by province and district[61]Last updated: 16 Nov 10:51 AM NPT
## 1 Cases
## 2 27,438
## 3 12,074
## 4 8,397
## 5 4,304
## 6 614
## [show all]vteCOVID-19 cases in Nepal by province and district[61]Last updated: 16 Nov 10:51 AM NPT
## 1 Deaths
## 2 192
## 3 62
## 4 71
## 5 30
## 6 8
Rename columns and delete first row
names(covid19_nepal) <- c("location", "date", "cases", "deaths")#"recovered"
## delete first row
covid19_nepal <- covid19_nepal[-1,]
Remove date column and Filter Province
covid19_nepal <- covid19_nepal %>% select(location, cases, deaths) %>% #recovered
filter(str_detect(location, "^Province")|
str_detect(location, "Bagmati")|
str_detect(location, "Gandaki")|
str_detect(location, "Karnali")|
str_detect(location, "Sudurpashchim"))
Add P_ID column
covid19_nepal <- covid19_nepal %>% mutate(P_ID = row_number())
Pull province map data from github & joining with covid19_data
province_map <- topojson_read("https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/province.topojson") %>%
arrange(D_ID)
## Reading layer `nepal' from data source `https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/province.topojson' using driver `TopoJSON'
## Simple feature collection with 7 features and 3 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 80.06014 ymin: 26.34752 xmax: 88.20401 ymax: 30.44702
## CRS: NA
province_map_joined <- geo_join(province_map, covid19_nepal, 'D_ID', 'P_ID')
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
province_map_joined <- province_map_joined %>%
mutate(cases = as.numeric(gsub(",","",province_map_joined$cases)),
#recovered = as.numeric(gsub(",","",province_map_joined$recovered)),
deaths = as.numeric(gsub(",","",province_map_joined$deaths)),
cases_fac = as.factor(gsub(",","",province_map_joined$cases)),
#recovered_fac = as.factor(gsub(",","",province_map_joined$recovered)),
deaths_fac = as.factor(gsub(",","",province_map_joined$deaths)))
str(province_map_joined)
## Classes 'sf' and 'data.frame': 7 obs. of 10 variables:
## $ id : chr NA NA NA NA ...
## $ Title : chr "Province No. 1" "Province No. 2" "Province No. 3" "Province No. 4" ...
## $ D_ID : int 1 2 3 4 5 6 7
## $ location : chr "Province No. 1" "Province No. 2" "Bagmati" "Gandaki" ...
## $ cases : num 27438 20106 128754 14482 25912 ...
## $ deaths : num 192 196 774 103 202 23 39
## $ rank : int 1 1 1 1 1 1 1
## $ geometry :sfc_POLYGON of length 7; first list element: List of 1
## ..$ : num [1:1868, 1:2] 86.9 86.9 86.9 86.9 86.9 ...
## ..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
## $ cases_fac : Factor w/ 7 levels "11957","128754",..: 6 4 2 3 5 7 1
## $ deaths_fac: Factor w/ 7 levels "103","192","196",..: 2 3 7 1 4 5 6
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA
## ..- attr(*, "names")= chr [1:9] "id" "Title" "D_ID" "location" ...
## - attr(*, "tigris")= logi NA
Visualising Corona’s effect on Nepal’s Provinces
palCases <- colorFactor("Paired", NULL)
#palRecovered <- colorFactor("Dark2", NULL)
palDeaths <- colorFactor("Accent", NULL)
labels <- sprintf(
"<strong>%s</strong> <br/>%g cases <br/>%g deaths",#%g recovered <br/>
province_map_joined$location, province_map_joined$cases, #province_map_joined$recovered,
province_map_joined$deaths) %>% lapply(htmltools::HTML)
province_map_joined %>%
leaflet() %>%
setView(lat = 28.3949, lng = 84.1240, zoom = 7) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(label = labels ,
color = '#444444',
weight = 1, smoothFactor = 0.5,
opacity = 1, fillOpacity = 0.50,
fillColor = ~palCases(cases_fac),
highlightOptions = highlightOptions(color = "white",
weight = 2,bringToFront = TRUE),
group = "Cases") %>%
#addPolygons(label = labels ,
#color = '#444444',
#weight = 1, smoothFactor = 0.5,
#opacity = 1, fillOpacity = 0.50,
#fillColor = ~palRecovered(recovered_fac),
#highlightOptions = highlightOptions(color = "white",
#weight = 2,bringToFront = TRUE),
#group = "Recovered") %>%
addPolygons(label = labels ,
color = '#444444',
weight = 1, smoothFactor = 0.5,
opacity = 1, fillOpacity = 0.50,
fillColor = ~palDeaths(deaths_fac),
highlightOptions = highlightOptions(color = "white",
weight = 2,bringToFront = TRUE),
group = "Deaths") %>%
addLegend(pal = palCases,
values = ~cases_fac,
opacity = 0.75,
title = "Cases",
position = "bottomright",
group = "CasesLeg") %>%
#addLegend(pal = palRecovered,
#values = ~recovered_fac,
#opacity = 0.75,
#title = "Recovered",
#position = "bottomright",
#group = "RecoveredLeg") %>%
addLegend(pal = palDeaths,
values = ~deaths_fac,
opacity = 0.75,
title = "Death",
position = "bottomright",
group = "DeathLeg") %>%
addLayersControl(baseGroups = c("Cases", "Deaths"), #"Recovered"
overlayGroups = c("CasesLeg", "DeathLeg"),#"RecoveredLeg",
options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup(c("DeathLeg"))#"RecoveredLeg",