Interactive Maps with leaflet

Introduction

In 1854 a Cholera epidemic haunts London. By then common theory is that Cholera is transmitted by air. English physician John Snow doubts this theory and is maybe the first one to perform geospatial analysis. He plot locations of Cholera-deaths and pumps and finds the causal link between water and Cholera. We will take a look at his results and on the fly learn how to use leaflet package.

  • Objectives: leaflet, geospatial
  • Requirements: R Data-Mining

Data Import

You need to download data of Cholera Pumps & Deaths from the link in Bibliography at the end of this article and save .csv file locally.

# Import Data
deaths_pumps <- read.csv("./data/Cholera Pumps & Deaths.csv")

Data Preparation

For our tutorial we need two packages:

  • leaflet for geospatial plotting
  • dplyr for data preparation
library(leaflet)
suppressPackageStartupMessages(library(dplyr))

geometry variable in deaths_pumps requires an extraction of longitude and latitude coordinates. Some irrelevant information is removed with gsub. Then, longitude and latitude information is extracted with strsplit, unlist and as.numeric. Coordinates (long and lat) are stored in one long vector coords. But they are easy to split: longitudes are small (below zero) and latitudes above 50.

Then, dataframe deaths_pumps is separated into deaths and pumps. Deaths are indicated by a count above 0 and pumps by a count below 0.

deaths_pumps$geometry <- gsub("<Point><coordinates>", "", deaths_pumps$geometry)
deaths_pumps$geometry <- gsub("</coordinates></Point>", "", deaths_pumps$geometry)
coords <- deaths_pumps$geometry %>% strsplit(., ",") %>% unlist() %>% as.numeric()

deaths_pumps$long <- coords[coords < 2]
deaths_pumps$lat <- coords[coords > 50]
deaths <- deaths_pumps %>% filter (count > 0)
pumps <- deaths_pumps %>% filter (count < 0)

median_location <- data.frame(long = median(deaths$long), 
             lat = median(deaths$lat))

Create Geospatial Plot

We create an object lf which will include all our information on the plot. First, we apply function leaflet and make extensive use of piping operator to load further information. We use Stamen.Toner to get the more classical black and white look. With setView we define the center and zoom of our map. We add circles for deaths and pumps.

Deaths are marked in red. With increasing death count radius increases. Pumps are marked in green.

lf <- leaflet() %>%
    addProviderTiles("Stamen.Toner") %>% 
    setView(lng = median_location$long, lat = median_location$lat, zoom = 17) %>% 
    addCircles(lng=deaths$long, 
           lat=deaths$lat, 
           radius = deaths$count*2, 
           stroke = F, 
           color = "red",
           fillOpacity = 0.8,
           popup= paste("Deaths: ", deaths$count)) %>% 
    addCircles(lng=pumps$long,
           lat=pumps$lat, 
           radius = 2, 
           color = "green",
           fillOpacity = 1,
           popup= "Pump")

lf  # Print the map

plot of chunk unnamed-chunk-4

You can see an interactive version of the map by clicking on it.

Conclusion

With this visualisation Snow was able to detect certain hotspots. In Broadwick Street (then: Broad Street) there is a pump and many Cholera-related deaths, while in other regions there are pumps and no deaths at all. Based on this he was able to refuse theory of air-borne Cholera transfer. He put his theory to test by disabling pump in Broad Street and saved many lifes.

We learned how easy it is to reproduce his results with leaflet.

Bibliography

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close