require("htmlwidgets")
require("leaflet")
require("rgdal")
require("rtweet")
require(RJSONIO)

1. Address Distribution of Hong Kong Confirmed cases

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)

2. COVID-19 Confirmed Cases in China

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
saveWidget(m, file="chinamap.html",selfcontained = FALSE)

3. Twitter User’s Locations

We finally use Twitter API to search for tweets according to the user location, i.e. 10,000 miles surrounding us.

#tweet <- search_tweets(" ",n=10000,geocode = "22.39,114.14,10000mi")
tweet <- readRDS("tweet.rds") # Or you can use the dataset in the Course Moodle

## Extract the coordinates
t_geo <- t(sapply(tweet$geo_coords,"["))

## Remove all NA rows
with_coord <- !is.na(t_geo[,1])
lat_lon <- data.frame(lat=t_geo[with_coord,1],lng=t_geo[with_coord,2])
lat_lon$popup <- paste0('At  ',tweet$created_at[with_coord],',',tweet$screen_name[with_coord],' said, " ',tweet$text[with_coord],'"')

## Show the first three columns
lat_lon[,c("lat","lng")]
##         lat      lng
## 1  11.95969 121.9267
## 2  14.57218 121.1879
## 3  14.23936 121.1518
## 4  14.85194 120.2593
## 5  14.56311 121.0689
## 6  16.23996 120.6262
## 7  14.60796 121.0803
## 8  14.57167 121.0236
## 9  14.54195 121.0556
## 10 17.32980 120.4455
## 11 14.64369 121.0312
## 12 22.28413 114.1482
## 13 14.53889 121.0577
## 14 11.98162 121.9163
## 15 13.11422 123.6468
## 16 14.73678 121.0553
## 17 15.47550 120.5963
## Call the Map and add the default tile
m <- leaflet()
m <- addTiles(m)
m <- addMarkers(m,data=lat_lon,lng=~lng,lat=~lat,popup=~popup)
m