library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(ggrepel)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(pander)
library(ggmap)

# https://www.andrewheiss.com/blog/2017/08/10/exploring-minards-1812-plot-with-ggplot2/

cities <- read.table("data/minard/cities.txt",
                     header = TRUE, stringsAsFactors = FALSE)

troops <- read.table("data/minard/troops.txt",
                     header = TRUE, stringsAsFactors = FALSE)

temps <- read.table("data/minard/temps.txt",
                    header = TRUE, stringsAsFactors = FALSE) %>%
  mutate(date = dmy(date))  # Convert string to actual date

temps.nice <- temps %>%
  mutate(nice.label = paste0(temp, "°, ", month, ". ", day))
font_family = "Helvetica"
color_cities = "black" #"#DC5B44"

color_troops_fw = "#DFC17E"
color_troops_bw = "#252523"
# No map this time
march.1812.plot.simple <- ggplot() +
  geom_path(data = troops, aes(x = long, y = lat, group = group, 
                               color = direction, size = survivors),
            lineend = "round") +
  geom_point(data = cities, aes(x = long, y = lat),
             color = color_cities) +
  geom_text_repel(data = cities, aes(x = long, y = lat, label = city),
                  color = color_cities, family = font_family) +
  scale_size(range = c(0.5, 15)) + 
  scale_colour_manual(values = c(color_troops_fw,color_troops_bw )) +
  guides(color = FALSE, size = FALSE) +
  theme_nothing()

# Change the x-axis limits to match the simple map
temps.1812.plot <- ggplot(data = temps.nice, aes(x = long, y = temp)) +
  geom_line() +
  geom_text_repel(aes(label = nice.label),
            family = font_family, size = 2.5) + 
  labs(x = NULL, y = "° Celsius") +
  scale_x_continuous(limits = ggplot_build(march.1812.plot.simple)$layout$panel_ranges[[1]]$x.range) +
  scale_y_continuous(position = "right") +
  coord_cartesian(ylim = c(-35, 5)) +  # Add some space above/below
  theme_bw(base_family = font_family) +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.text.x = element_blank(), axis.ticks = element_blank(),
        panel.border = element_blank())

# Combine the two plots
both.1812.plot.simple <- rbind(ggplotGrob(march.1812.plot.simple),
                               ggplotGrob(temps.1812.plot))

# Adjust panels
panels <- both.1812.plot.simple$layout$t[grep("panel", both.1812.plot.simple$layout$name)]

# Because this plot doesn't use coord_equal, since it's not a map, we can use whatever relative numbers we want, like a 3:1 ratio
both.1812.plot.simple$heights[panels] <- unit(c(3, 1), "null")

grid::grid.newpage()
grid::grid.draw(both.1812.plot.simple)

march.1812.europe <- c(left = -13.10, bottom = 35.75, right = 41.04, top = 61.86)

march.1812.europe.map.wc <- get_stamenmap(bbox = march.1812.europe, zoom = 5,
                                          maptype = "toner", where = "cache")
## Source : http://tile.stamen.com/toner/5/14/8.png
## Source : http://tile.stamen.com/toner/5/15/8.png
## Source : http://tile.stamen.com/toner/5/16/8.png
## Source : http://tile.stamen.com/toner/5/17/8.png
## Source : http://tile.stamen.com/toner/5/18/8.png
## Source : http://tile.stamen.com/toner/5/19/8.png
## Source : http://tile.stamen.com/toner/5/14/9.png
## Source : http://tile.stamen.com/toner/5/15/9.png
## Source : http://tile.stamen.com/toner/5/16/9.png
## Source : http://tile.stamen.com/toner/5/17/9.png
## Source : http://tile.stamen.com/toner/5/18/9.png
## Source : http://tile.stamen.com/toner/5/19/9.png
## Source : http://tile.stamen.com/toner/5/14/10.png
## Source : http://tile.stamen.com/toner/5/15/10.png
## Source : http://tile.stamen.com/toner/5/16/10.png
## Source : http://tile.stamen.com/toner/5/17/10.png
## Source : http://tile.stamen.com/toner/5/18/10.png
## Source : http://tile.stamen.com/toner/5/19/10.png
## Source : http://tile.stamen.com/toner/5/14/11.png
## Source : http://tile.stamen.com/toner/5/15/11.png
## Source : http://tile.stamen.com/toner/5/16/11.png
## Source : http://tile.stamen.com/toner/5/17/11.png
## Source : http://tile.stamen.com/toner/5/18/11.png
## Source : http://tile.stamen.com/toner/5/19/11.png
## Source : http://tile.stamen.com/toner/5/14/12.png
## Source : http://tile.stamen.com/toner/5/15/12.png
## Source : http://tile.stamen.com/toner/5/16/12.png
## Source : http://tile.stamen.com/toner/5/17/12.png
## Source : http://tile.stamen.com/toner/5/18/12.png
## Source : http://tile.stamen.com/toner/5/19/12.png
ggmap(march.1812.europe.map.wc) +
  geom_path(data = troops, aes(x = long, y = lat, group = group, 
                               color = direction, size = survivors),
            lineend = "round") +
  scale_size(range = c(0.5, 5)) + 
  scale_colour_manual(values = c("#DFC17E", "#252523")) +
  guides(color = FALSE, size = FALSE) +
  theme_nothing()  # This is a special theme that comes with ggmap