---
title: "Geospatial Data<br>Example: C.J. Minard&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 = "../../../scripts/slide-setup.Rmd"}
```

```{r echo=FALSE, warning=FALSE, message=FALSE}
options(dplyr.print_max = 12)
library(tidyverse)
```

# C.J. Minard&rsquo;s Famous &ldquo;Carte Figurative&rdquo;

![Minards Map](images/Minard.png)

.footnote[Image from: [Wikimedia.org](https://upload.wikimedia.org/wikipedia/commons/2/29/Minard.png)]

???
As explained on the map (in French), this map depicts various aspects of Napoleon's (ill-fated), 1812-1813 invasion of Russia.

Consider the elements of the visualization.

- What do the following features represent?
  - points
  - lines
  - line color
  - line width
  - the line chart at the bottom
- How would we reproduce this? 
  - What gets geocoded?
  - Can we reproduce it all?

See J. Lacko's notes for the challenges he faced in addressing idiosyncrasies in the data.

---
# J. Lacko&rsquo;s Geocoded Reproduction

```{r map, echo=FALSE, error=FALSE, message=FALSE, warning=FALSE, results="hide", fig.show="hide"}
library(tidyverse)
library(tidygeocoder)

minard_raw <- data.frame(city = c("Kaunas", "Vilnius", "Глубокое", "Витебск", 
                                  "Смоленск", "Дорогобу́ж", "Гага́рин"  ,
                                  "Можа́йск",  "Москва", "Москва", 
                                  "Малояросла́вец", "Можа́йск", "Вя́зьма",
                                  "Дорогобу́ж", "Смоленск", "О́рша",
                                  "Бобр", "Бори́сов",  "Маладзечна",
                                  "Сморгонь", "Vilnius", "Kaunas", # La Grande Armée
                                  "Vilnius", "Daugavpils",  
                                  "По́лоцк", "Бобр", # reserve in Polotskt
                                  "Kaunas", "Riga", 
                                  "Riga", "Kaunas"), # the siege of Riga
                     direction = c("outward", "outward", "outward", "outward",
                                   "outward", "outward", "outward", "outward",
                                   "outward", "homeward", "homeward", "homeward",
                                   "homeward", "homeward", "homeward", "homeward",
                                   "homeward", "homeward", "homeward", "homeward",
                                   "homeward", "homeward", # Napoleon
                                   "outward", "outward", "homeward",
                                   "homeward", # Oudinot
                                   "outward", "outward", "homeward", 
                                   "homeward"), # MacDonald
                     strength = c(422000, 340000, 280000, 210000, 145000,
                                  140000, 127100, 100000, 100000, 100000,
                                  98000, 87000, 55000, 37000, 24000, 20000,
                                  50000, 28000, 12000, 14000, 8000,
                                  4000, # Napoleon
                                  60000, 40000, 33000, 28000, # Oudinot
                                  22000, 22000, 6000, 6000), # MacDonald
                     leader = c(rep("Napoleon", 22),  
                                rep("Oudinot", 4), 
                                rep("MacDonald", 4))) 

minard_geocoded <- read_csv("data/menard-scraped-data.csv")

#  city labels
city_labels <- minard_geocoded %>%
  select(city, lat, long) %>% 
  distinct()

# offsets - a kludge to make the labels legible
kludge <- data.frame(horizontal = c(0, -.25, -.2, 0, 0,
                                    .25, -.5, 0, 0, 0,
                                    .4, .25, .4, -.2, -.2,
                                    .7, -.25, .4, 0),
                     vertical = c(-.2, -.2, .3, .3, -.3,
                                  -.3, .2, .2, .2, -.2,
                                  -.3, -.2, -.15, .2, -.15,
                                  .1, .2, .2, .15))

library(ggmap)

basemap <- get_stamenmap(bbox = c(left =  23, bottom = 53.75, right = 39.25, top = 57.15),
                         maptype = "toner", zoom = 7)

ggmap(basemap) +
 geom_path(data = minard_geocoded, 
            aes(x = long, y = lat, size = strength, color = direction, 
                group = factor(leader, levels = c("Oudinot", 
                                                  "MacDonald",
                                                  "Napoleon"))),
           alpha = 2/3) +
  scale_colour_manual(values = c("gray10", "#e8cbac")) +
  scale_size(range = c(1/4, 17)) +
  guides(color = F, size = F) +
  theme_void() +
  labs(title = "1812, or There and Back Again",
       subtitle = "geocoding the Minard's map") +
  theme(plot.title = element_text(hjust = 1/2, face = "bold"),
        plot.subtitle = element_text(hjust = 1/2, face = "italic"))
```

```{r ref.label="map", echo=FALSE, message=FALSE, warning=FALSE, output.lines=10, fig.align="left", out.width="75%"}
```

.footnote[Example from J. Lacko&rsquo;s wonderful techblog: https://www.jla-data.net/eng/minard-map-tidygeocoder/]

???
Demo this code using `minard-demo.Rmd`. (It's too hard to fit on slides!)

Save the interactive map for the very end.
