Mapa apilado en ggplot
Introducción
En este post se muestra como deformar y apilar mapas para dar la impresión de mapas superpuestos en diferentes capas usando ggplot. Este código esta basado en el gist publicado por Rafael Pereira. ¡Gracias Rafa!
Las librerías necesarias para reproducir el ejemplo son:
library(tidyverse) # Easily Install and Load the 'Tidyverse'
library(ggnewscale) # Multiple Fill and Colour Scales in 'ggplot2'
library(gganimate) # A Grammar of Animated Graphics
library(hrbrthemes) # Additional Themes, Theme Components and Utilities for 'ggplot2'
library(rgdal) # Bindings for the 'Geospatial' Data Abstraction Library
library(pals) # Color Palettes, Colormaps, and Tools to Evaluate Them
library(transformr) # Polygon and Path TransformationsLos datos empleados en para este ejemplo los puedes descargar aquí. Los datos fueron extraídos del sitio web del ministerio de Salud y Protección Social de la República de Colombia , se reporta el número de casos de pacientes fallecidos, activos y recuperados por género que contrayeron COVID-19 para cada departamento hasta el día 10-10-2020.
# Translation to center of coordinate system ------------------------------
mapa.covid <-
read_csv("files/datos.csv") %>%
select(-id)
mapa.covid %>% head() %>%
kable(align = "c") %>%
kable_styling("striped", full_width = TRUE)| long | lat | group | Cod.Dep | Departamento | Tipo | F | M | Casos | rango |
|---|---|---|---|---|---|---|---|---|---|
| -76.95417 | 8.185339 | 0.1 | 5 | Antioquia | Activo | 8792 | 8382 | 17174 | 16000 - 18000 |
| -76.95417 | 8.185339 | 0.1 | 5 | Antioquia | Recuperado | 56817 | 57650 | 114467 | 1e+05 - 120000 |
| -76.95417 | 8.185339 | 0.1 | 5 | Antioquia | Fallecido | 1046 | 1655 | 2701 | 2000 - 3000 |
| -76.93473 | 8.188839 | 0.1 | 5 | Antioquia | Activo | 8792 | 8382 | 17174 | 16000 - 18000 |
| -76.93473 | 8.188839 | 0.1 | 5 | Antioquia | Recuperado | 56817 | 57650 | 114467 | 1e+05 - 120000 |
| -76.93473 | 8.188839 | 0.1 | 5 | Antioquia | Fallecido | 1046 | 1655 | 2701 | 2000 - 3000 |
mapa.covid <-
mapa.covid %>%
mutate(long = long - mean(long),
lat = lat - mean(lat))
# Shear effect and Rotation Matrix ------------------------------
# Shear Matrix
shear_matrix <-
matrix(c(1,0,0.7,0.5),2,2) %>% t
# Rotation matrix
rotation_matrix <-
function(theta){
matrix(c(cos(theta*pi/180), -sin(theta*pi/180),
sin(theta*pi/180), cos(theta*pi/180)),
nrow = 2, ncol = 2, byrow = T) %>%
round(digits = 2)
}