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

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 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vT6aoKk3iHmotqb5_iHggKc_3uAA901xVzwsllmNoOpGgRZ8VAA3TSxK6XreKzg_AUQXIkVX5rqb0Mo/pub?gid=0&range=A2:ZZ&output=csv")

hk_cc <- hk_cc[!is.na(hk_cc$lat),] # remove all NA coordinates
hk_cc <- hk_cc[!is.na(as.integer(hk_cc$case_no)),] # remove all NA case number

hk_cc <- hk_cc[as.integer(hk_cc$case_no) >= 11300,] # Case 11300+ only, ignore warning

popup <- paste0("End Date: ",hk_cc$end_date,"\nAddress: ",hk_cc$location_en," \nSource:",hk_cc$source_url_1)

hk_cc_latlng <- data.frame(lat=as.numeric(hk_cc$lat),lng=as.numeric(hk_cc$lng),pop_up = popup,stringsAsFactors = F)


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 (March 19)

Next, we create a map displaying 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)
##  chr [1:31] "Anhui" "Beijing" "Chongqing" "Fujian" "Gansu" "Guangdong" ...
# 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 14.67604 121.0437
## 2 14.70738 121.0256
## 3 13.62526 123.1843
## 4 11.92460 121.9521
## 5 16.41501 120.5960
## 6 13.41700 120.4670
## 7 11.46700 124.9500
## 8 15.09560 120.8267
## 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