require("htmlwidgets")
require("leaflet")
require("rgdal")
require("rtweet")
require(RJSONIO)
First, We build a map to show the COVID-19 confirmed cases in Hong Kong.
# Source: https://wars.vote4.hk/
hk_cc <- fromJSON("https://wars.vote4.hk/page-data/high-risk/page-data.json")
hk_cc_latlng <- lapply(hk_cc$result$data$allWarsCaseLocation$edges,function(x){c(x$node$lat,x$node$lng,paste0("Address: ",x$node$location_en," \nSource:",x$node$source_url_1))})
hk_cc_latlng <- do.call(rbind,hk_cc_latlng) # Convert to data.frame
hk_cc_latlng <- data.frame(lat=as.numeric(hk_cc_latlng[,1]),lng=as.numeric(hk_cc_latlng[,2]),pop_up=hk_cc_latlng[,3],stringsAsFactors = F)
hk_cc_latlng <- hk_cc_latlng[!is.na(hk_cc_latlng$lat),] # remove all NA coordinates
hk_cc_latlng <- hk_cc_latlng[hk_cc_latlng[,1]>20,] # renove an outlier
m <- leaflet() # map initialization
m <- addTiles(m) # add the basemap
m <- addMarkers(m, data=hk_cc_latlng,lng=~lng, lat=~lat, popup=~pop_up)
m
Last, a html page is exported for you to embed it into your site/wordpress by iframe inclusion.
saveWidget(m, file="hk_cc_map.html",selfcontained = FALSE)
Next, we create a map dispalying the provincial distribution of COVID-19 Confirmed Cases in China.
chn_adm <- readOGR("./CHN_adm/CHN_adm1.shp", layer = "CHN_adm1", GDAL1_integer64_policy = TRUE)
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\kwfu.JMSC\Documents\JMSC\Social Media analysis\CHN_adm\CHN_adm1.shp", layer: "CHN_adm1"
## with 31 features
## It has 9 fields
## Integer64 fields read as doubles: ID_0 ID_1
dxy_cc <- read.csv("https://raw.githubusercontent.com/globalcitizen/2019-wuhan-coronavirus-data/master/data-sources/dxy/data/20200319-013132-dxy-2019ncov-data.csv",sep="|",skip=3,header=F,stringsAsFactors = F)
dxy_cc <- dxy_cc[1:34,1:3] # Keep the first three columns and exclude "CHINA TOTAL"
colnames(dxy_cc) <- c("Place","confirmed_cases","deaths")
str(chn_adm$NAME_1)
## Factor w/ 31 levels "Anhui","Beijing",..: 1 2 3 4 5 6 7 8 9 10 ...
# Clean the data to make sure the names in both datasets are matched
dxy_cc[dxy_cc$Place == "Inner Mongolia","Place"] <- "Nei Mongol"
dxy_cc[dxy_cc$Place == "Ningxia","Place"] <- "Ningxia Hui"
dxy_cc[dxy_cc$Place == "Xinjiang","Place"] <- "Xinjiang Uygur"
dxy_cc[dxy_cc$Place == "Tibet","Place"] <- "Xizang"
# Add two new columns to the OGR data
chn_adm$confirmed_cases <- dxy_cc$confirmed_cases[match(as.character(chn_adm$NAME_1),dxy_cc$Place)]
chn_adm$deaths <- dxy_cc$deaths[match(as.character(chn_adm$NAME_1),dxy_cc$Place)]
m <- leaflet()
m <- addTiles(m)
pal_m <- colorBin(palette="Reds",bin = c(0,100,500,1000,2000,5000,10000,20000,40000,60000,80000), domain=chn_adm$confirmed_cases)
chn_adm$pop_up <- paste0(chn_adm$NAME_1,"- comfirmed cases: ",chn_adm$confirmed_cases)
m <- addPolygons(m,data=chn_adm,weight = 1,popup=~pop_up,color = ~pal_m(chn_adm$confirmed_cases))
m <- addLegend(m, "topright", pal = pal_m, values = chn_adm$confirmed_cases, title = "COVID-19 Confirmed Cases", opacity = 0.75)
m