No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

103 líneas
3.2KB

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