Skip to content

Instantly share code, notes, and snippets.

@nxrunning
Last active May 30, 2020 08:00
Show Gist options
  • Save nxrunning/8cddd07506a90a8a9c29981abdf9bbed to your computer and use it in GitHub Desktop.
Save nxrunning/8cddd07506a90a8a9c29981abdf9bbed to your computer and use it in GitHub Desktop.
Spatial visualisation of Covid-19 clusters in Singapore with ggmap
# Import libraries
library(ggmap)
library(ggplot2)
library(rvest)
# Authorise google API
register_google(key = "Insert your google maps API key here")
#### Web scraping of cluster list data ####
# Url for singapore covid-19 cluster information
url <- "https://co.vid19.sg/singapore/"
# Scraped page
scraped_page <- read_html(url)
# Scraping the clusters column
clusters <- scraped_page %>%
html_nodes((".table-dashboard-one .tx-medium")) %>%
html_text() %>%
as.character()
# Scraping the number of cases column
cases <- scraped_page %>%
html_nodes((".table-dashboard-one td.text-right")) %>%
html_text() %>%
as.character()
#### Data Processing ####
# Combining scraped data into a dataframe
cluster_df <- data.frame(clusters, cases)
cluster_df$lon <- NA
cluster_df$lat <- NA
# Convert cluster column to string format
cluster_df$clusters <- as.character(cluster_df$clusters)
# Convert cases column to integer format
cluster_df$cases <- as.integer(as.character(cluster_df$cases))
# Remove the "mass religious gathering in Malaysia" cluster since it does not have an exact address
cluster_df <- cluster_df[(cluster_df$clusters != "Mass Religious Gathering In Malaysia"),]
rownames(cluster_df) <- 1:nrow(cluster_df)
# Edit the list to better find the respective locations
cluster_df[(cluster_df$clusters == "Grand Hyatt Hotel"), c("clusters")] <- "Grand Hyatt Hotel Singapore"
cluster_df[(cluster_df$clusters == "Grace Assembly of God"), c("clusters")] <- "Grace Assembly of God Singapore"
cluster_df[(cluster_df$clusters == "Private dinner function at SAFRA Jurong"), c("clusters")] <- "SAFRA Jurong"
cluster_df[(cluster_df$clusters == "Cochrane Lodge I"), c("clusters")] <- "Cochrane Lodge I Singapore"
cluster_df[(cluster_df$clusters == "Black Tap"), c("clusters")] <- "Black Tap Singapore"
cluster_df[(cluster_df$clusters == "North Coast Lodge"), c("clusters")] <- "North Coast Lodge Singapore"
cluster_df[(cluster_df$clusters == "Acacia Lodge"), c("clusters")] <- "Acacia Lodge Singapore"
cluster_df[(cluster_df$clusters == "McDonald’s"), c("clusters")] <- "McDonald's Singapore"
cluster_df[(cluster_df$clusters == "Project Glory"), c("clusters")] <- "Project Glory Singapore"
cluster_df[(cluster_df$clusters == "Keppel Shipyard"), c("clusters")] <- "Keppel Shipyard Singapore"
cluster_df[(cluster_df$clusters == "Orange Ballroom"), c("clusters")] <- "Orange Ballroom Singapore"
cluster_df[(cluster_df$clusters == "The Wedding Brocade"), c("clusters")] <- "The Wedding Brocade Singapore"
cluster_df[(cluster_df$clusters == "Mustafa Centre"), c("clusters")] <- "Mustafa Centre Singapore"
#### Geocoding of each cluster address ####
# Computing latitude and longitude of each location
for (i in 1:nrow(cluster_df)) {
cluster_name = cluster_df[i, c("clusters")]
cluster_geocode = geocode(cluster_name)
cluster_df[i, c("lon")] <- cluster_geocode$lon
cluster_df[i, c("lat")] <- cluster_geocode$lat
}
#### Map visualisation ####
# Locating the latitude and longitude position of Singapore map
# Lon = 103.8198, Lat = 1.352083
singapore_city <- geocode("Singapore")
# Retrieves the map image of the location
# You may specify the zoom size and maptype
singapore_map <- get_map(singapore_city, zoom = 12, maptype = "roadmap")
# Visualise Singapore map
mapplot <- ggmap(singapore_map)
# Map the clusters on singapore map
# Specify gradient colours using scale_colour_gradient
# theme_void() to remove the longitude and latitude axes
cluster_plot <-ggmap(singapore_map,
base_layer = ggplot(data = cluster_df,
aes(x=lon, y = lat, color = cases))) +
geom_point(size = 1.5,
alpha = 0.8)+
labs(title = "Covid-19 Clusters in Singapore",
caption = "Data from Covid-19 Singapore Dashboard",
color = "Number of cases")+
scale_colour_gradient(low = "blue", high = "red", na.value = NA)+
theme_void()
# Save the plot
ggsave(cluster_plot, dpi = 600, filename = "filename.jpeg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment