選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

106 行
3.5KB

  1. ---
  2. title: "Minard"
  3. author: "Antoine Neuraz"
  4. date: "18/11/2019"
  5. output: html_document
  6. ---
  7. ```{r, include=FALSE}
  8. knitr::opts_chunk$set(echo = TRUE)
  9. ```
  10. ```{r}
  11. library(tidyverse)
  12. library(lubridate)
  13. library(ggmap)
  14. library(ggrepel)
  15. library(gridExtra)
  16. library(pander)
  17. library(ggmap)
  18. # https://www.andrewheiss.com/blog/2017/08/10/exploring-minards-1812-plot-with-ggplot2/
  19. cities <- read.table("data/minard/cities.txt",
  20. header = TRUE, stringsAsFactors = FALSE)
  21. troops <- read.table("data/minard/troops.txt",
  22. header = TRUE, stringsAsFactors = FALSE)
  23. temps <- read.table("data/minard/temps.txt",
  24. header = TRUE, stringsAsFactors = FALSE) %>%
  25. mutate(date = dmy(date)) # Convert string to actual date
  26. temps.nice <- temps %>%
  27. mutate(nice.label = paste0(temp, "°, ", month, ". ", day))
  28. ```
  29. ```{r, fig.asp=.4}
  30. font_family = "Helvetica"
  31. color_cities = "black" #"#DC5B44"
  32. color_troops_fw = "#DFC17E"
  33. color_troops_bw = "#252523"
  34. # No map this time
  35. march.1812.plot.simple <- ggplot() +
  36. geom_path(data = troops, aes(x = long, y = lat, group = group,
  37. color = direction, size = survivors),
  38. lineend = "round") +
  39. geom_point(data = cities, aes(x = long, y = lat),
  40. color = color_cities) +
  41. geom_text_repel(data = cities, aes(x = long, y = lat, label = city),
  42. color = color_cities, family = font_family) +
  43. scale_size(range = c(0.5, 15)) +
  44. scale_colour_manual(values = c(color_troops_fw,color_troops_bw )) +
  45. guides(color = FALSE, size = FALSE) +
  46. theme_nothing()
  47. # Change the x-axis limits to match the simple map
  48. temps.1812.plot <- ggplot(data = temps.nice, aes(x = long, y = temp)) +
  49. geom_line() +
  50. geom_text_repel(aes(label = nice.label),
  51. family = font_family, size = 2.5) +
  52. labs(x = NULL, y = "° Celsius") +
  53. scale_x_continuous(limits = ggplot_build(march.1812.plot.simple)$layout$panel_ranges[[1]]$x.range) +
  54. scale_y_continuous(position = "right") +
  55. coord_cartesian(ylim = c(-35, 5)) + # Add some space above/below
  56. theme_bw(base_family = font_family) +
  57. theme(panel.grid.major.x = element_blank(),
  58. panel.grid.minor.x = element_blank(),
  59. panel.grid.minor.y = element_blank(),
  60. axis.text.x = element_blank(), axis.ticks = element_blank(),
  61. panel.border = element_blank())
  62. # Combine the two plots
  63. both.1812.plot.simple <- rbind(ggplotGrob(march.1812.plot.simple),
  64. ggplotGrob(temps.1812.plot))
  65. # Adjust panels
  66. panels <- both.1812.plot.simple$layout$t[grep("panel", both.1812.plot.simple$layout$name)]
  67. # 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
  68. both.1812.plot.simple$heights[panels] <- unit(c(3, 1), "null")
  69. grid::grid.newpage()
  70. grid::grid.draw(both.1812.plot.simple)
  71. ```
  72. ```{r}
  73. march.1812.europe <- c(left = -13.10, bottom = 35.75, right = 41.04, top = 61.86)
  74. march.1812.europe.map.wc <- get_stamenmap(bbox = march.1812.europe, zoom = 5,
  75. maptype = "toner", where = "cache")
  76. ggmap(march.1812.europe.map.wc) +
  77. geom_path(data = troops, aes(x = long, y = lat, group = group,
  78. color = direction, size = survivors),
  79. lineend = "round") +
  80. scale_size(range = c(0.5, 5)) +
  81. scale_colour_manual(values = c("#DFC17E", "#252523")) +
  82. guides(color = FALSE, size = FALSE) +
  83. theme_nothing() # This is a special theme that comes with ggmap
  84. ```