#COVID Data Extraction from Wikipedia
library("XML")
library("rvest")
## Loading required package: xml2
##
## Attaching package: 'rvest'
## The following object is masked from 'package:XML':
##
## xml
library("ggplot2")
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
np_webpage <- read_html('https://en.wikipedia.org/wiki/COVID-19_pandemic_in_Nepal')
cov_np <- np_webpage %>%
html_node(xpath = '//*[@id="covid19-container"]/table') %>% html_table(fill = TRUE)
#covid data cleaning
cov_np <- cov_np[-1,]
names(cov_np) <- c("district", "last_update", "confirmed", "recov", "death")
cov_np$confirmed <- gsub(",", "",cov_np$confirmed, fixed = TRUE)
cov_np$recov<- gsub(",", "",cov_np$recov , fixed = TRUE)
str(cov_np)
## 'data.frame': 86 obs. of 5 variables:
## $ district : chr "Province No. 1" "Jhapa" "Morang" "Sunsari" ...
## $ last_update: chr "17 Apr" "13 May" "17 May" "18 May" ...
## $ confirmed : chr "831" "310" "209" "112" ...
## $ recov : chr "403" "182" "92" "54" ...
## $ death : chr "0" "0" "0" "0" ...
cov_np$con <- as.numeric(cov_np$confirmed)
## Warning: NAs introduced by coercion
cov_np$death <- as.numeric(cov_np$death)
## Warning: NAs introduced by coercion
cov_np$recov <- as.numeric(cov_np$recov)
## Warning: NAs introduced by coercion
cov_np$district <- tolower(cov_np$district)
#Nepal Distirct Map Loading
library(leaflet)
library(geojsonio)
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
d_map <- topojson_read("https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/district.topojson")
## Reading layer `nepal' from data source `https://raw.githubusercontent.com/rugnepal/learnRgroup/master/week_13/district.topojson' using driver `TopoJSON'
## Simple feature collection with 75 features and 3 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 80.06014 ymin: 26.34752 xmax: 88.20401 ymax: 30.44702
## CRS: NA
#cleaning District data and merging datasets
library(tigris)
## To enable
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
df <- geo_join(d_map, cov_np, 'district', 'district')
## 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.
d_map[9,2] <- "terhathum"
d_map[30,2] <- "sindhupalchok"
d_map[39,2] <- "manang"
d_map[40,2] <- "kapilvastu"
d_map[69,2] <- "west rukum"
d_map[74,2] <- "nawalpur"
df <- geo_join(d_map, cov_np, 'district', 'district')
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
#Confirmed Cases
plot(df["con"])
#revovered Cases
plot(df["recov"])
#deaths mapping
plot(df["death"])
#Combining KTM valley
ktm <- df[df$district== "kathmandu", ]
lp <- df[df$district== "lalitpur", ]
bkt <- df[df$district== "bhaktapur", ]
valley <- rbind(ktm, bkt, lp)
#Confirmed Cases
plot(valley["con"])
Using Library Leaflet
library(leaflet)
cpal <- colorNumeric("Reds", df$con, na.color = "white", alpha=F, reverse = F)
leaflet(df) %>% addPolygons(stroke = F,opacity = 1,fillOpacity = 0.7, color = "blue", fillColor = cpal(df$con), weight = 1) %>% addLegend(values = df$con, pal = cpal, title = "Confirmed Cases")
Using Library tmap
library(tmap)
#with breaks
map_np1 = tm_shape(df) + tm_polygons(col = "con")
map_np1
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
#defining breaks
breaks = c(0,0.2,1, 3, 4, 5,7, 10) * 100
map_np2 = tm_shape(df) + tm_polygons(col = "con", breaks=breaks)
map2 <- map_np2 + tm_compass(type = "8star", position = c("left", "bottom")) +
tm_scale_bar(breaks = c(0, 100, 200), text.size = 1,position = c("left", "bottom"))
#Continuous
map_np3 = tm_shape(df) + tm_polygons(col = "con", style = "cont")
map1 <- map_np3 + tm_compass(type = "8star", position = c("left", "bottom")) +
tm_scale_bar(breaks = c(0, 100, 200), text.size = 1,position = c("left", "bottom"))+ tm_layout(title = "Nepal - Distribution of COVID-19 Confirmed cases", frame = F) + tm_dots(size = "death", col = "red")
map1+ tm_basemap(server = "OpenTopoMap")
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
tmap_mode("view")
## tmap mode set to interactive viewing
map2+tm_dots(size = "death", col = "red")
## Compass not supported in view mode.
## Warning: Currect projection of shape df unknown. Long-lat (WGS84) is assumed.
## Warning: Values have found that are higher than the highest break
## Legend for symbol sizes not available in view mode.
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.