---
title: "Geospatial Data<br>Example: J. Snow&rsquo;s Famous Map"
author: "Keith VanderLinden<br>Calvin University"
output:
  xaringan::moon_reader:
    nature:
      titleSlideClass: ["left", "top"]
      highlightLines: true
      ratio: "16:9"     
---

```{r child = "../../resources/scripts/slide-setup.Rmd"}
```

```{r packages, echo = FALSE, warning = FALSE, message = FALSE}
knitr::opts_chunk$set(eval = TRUE)
options(dplyr.print_max = 12)
```

# Snow&rsquo;s Map

.pull-left[
John Snow&rsquo;s analysis of a mid-1800s outbreak of cholera was based on his hand-drawn, geospatially-mapped data shown here.

.footnote[Image from: [Wikimedia.org](https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Snow-cholera-map-1.jpg/450px-Snow-cholera-map-1.jpg)).]
]
.pull-right[
![J. Snow&rsquo;s Cholera Map](https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Snow-cholera-map-1.jpg/450px-Snow-cholera-map-1.jpg)
]


???
- This is a distilled version of the textbook's demo.
- References:
  - https://en.wikipedia.org/wiki/John_Snow#Cholera
  - https://mdsr-book.github.io/mdsr2e/gfx/1280px-Snow-cholera-map-1.jpg
  - https://archive.org/details/b28985266/page/n57/mode/2up

---
# Snow&rsquo;s Data

.pull-left[
```{r output.lines = 8}
library(tidyverse)
library(mdsr)

CholeraDeaths
```

```{r snow-data, results="hide", fig.show = "hide", message = FALSE, warning = FALSE}
library(sf)

plot(CholeraDeaths["Count"])
```
]
.pull-right[
```{r ref.label = "snow-data", echo = FALSE, message = FALSE, warning = FALSE, output.lines = 10, out.width = "100%"}
```
]

???
- Compare and contrast this with Snow's original map.
- You can see what appear to be street lines, but this plot doesn't have the context provided by the map (vector or raster).
- The counts are indicated by color, not by repeated "bars" as in Snow's original plot.

---
# A Reproduction of Snow&rsquo;s Map

.pull-left[
```{r snow-map, error = FALSE, message = FALSE, warning = FALSE, results = "hide", fig.show = "hide"}
library(ggspatial)

dsn <- fs::path(fs::path_wd(), "..", "..", 
                "resources", "data", 
                "SnowGIS_SHP")
deaths <- st_read(dsn, 
                  layer = "Cholera_Deaths", 
                  quiet = TRUE) %>%
  st_set_crs(27700) %>%
  st_transform(4326)
pumps <- st_read(dsn, 
                 layer = "Pumps", 
                 quiet = TRUE) %>%
  st_set_crs(27700) %>%
  st_transform(4326)

deaths %>%
  ggplot() +
  annotation_map_tile(type="osm", zoomin=0)+
  geom_sf(aes(size = Count), alpha = 0.7)+
  geom_sf(data = pumps, size=3, color="red")
  
```
]
.pull-right[
```{r ref.label = "snow-map", echo = FALSE, message = FALSE, warning = FALSE, output.lines = 10, out.width = "100%"}
```
]

???
- This got ugly because the original data was in a UK format (27700), not WGS84 (4326). Hence, the need to transform it before layering it over the OSM tiles.
- We project the cholera data points onto the projection used by the map tile.
  - First, `st_set_crs()` projects onto EPSG:27700, as is suggested by the dataset.
  - Then, `st_transform()` projects onto EPSG:4326 (aka. WGS84), which is what we really want.
- Set `quiet = FALSE` to see useful st_read() output.
- Disentangle the CRS & transform required to get this projection right.
