Skip to content

Instantly share code, notes, and snippets.

@diegovalle diegovalle/mx_grid.R
Created Jun 30, 2017

Embed
What would you like to do?
mexico geofacet
library(dplyr)
library(stringr)
library(readr)
library(ggplot2)
library(geofacet)
options(stringsAsFactors = FALSE)
mx_grid <- data.frame(
code = c(2L, 8L, 26L, 3L, 5L, 10L, 19L, 25L, 28L, 1L, 18L, 24L, 32L,
11L, 13L, 14L, 22L, 30L, 6L, 15L, 29L, 9L, 17L, 21L, 31L, 4L,
12L, 16L, 20L, 23L, 27L, 7L),
name = c("Baja California", "Chihuahua", "Sonora", "Baja California Sur", "Coahuila", "Durango", "Nuevo León", "Sinaloa", "Tamaulipas", "Aguascalientes", "Nayarit", "San Luis Potosí", "Zacatecas", "Guanajuato", "Hidalgo", "Jalisco", "Querétaro", "Veracruz", "Colima", "México", "Tlaxcala", "Ciudad de México", "Morelos", "Puebla", "Yucatán", "Campeche", "Guerrero", "Michoacán", "Oaxaca", "Quintana Roo", "Tabasco", "Chiapas"),
name_official = c("Baja California", "Chihuahua", "Sonora", "Baja California Sur", "Coahuila de Zaragoza", "Durango", "Nuevo León", "Sinaloa", "Tamaulipas", "Aguascalientes", "Nayarit", "San Luis Potosí", "Zacatecas", "Guanajuato", "Hidalgo", "Jalisco", "Querétaro", "Veracruz de Ignacio de la Llave", "Colima", "México", "Tlaxcala", "Ciudad de México", "Morelos", "Puebla", "Yucatán", "Campeche", "Guerrero", "Michoacán de Ocampo", "Oaxaca", "Quintana Roo", "Tabasco", "Chiapas"),
name_abbr = c("BC", "CHIH", "SON", "BCS", "COAH", "DGO", "NL", "SIN", "TAM", "AGS", "NAY", "SLP", "ZAC", "GTO", "HGO", "JAL", "QRO", "VER", "COL", "MEX", "TLAX", "CDMX", "MOR", "PUE", "YUC", "CAMP", "GRO", "MICH", "OAX", "QROO", "TAB", "CHPS"),
name_abbr_iso = c("BCN", "CHH", "SON", "BCS", "COA", "DUR", "NLE", "SIN", "TAM", "AGU", "NAY", "SLP", "ZAC", "GUA", "HID", "JAL", "QUE", "VER", "COL", "MEX", "TLA", "CMX", "MOR", "PUE", "YUC", "CAM", "GRO", "MIC", "OAX", "ROO", "TAB", "CHP"),
name_abbr_official = c("BC", "Chih.", "Son.", "BCS", "Coah.", "Dgo.", "NL", "Sin.", "Tamps.", "Ags.", "Nay.", "SLP", "Zac.", "Gto.", "Hgo.", "Jal.", "Qro.", "Ver.", "Col.", "Mex.", "Tlax.", "CDMX", "Mor.", "Pue.", "Yuc.", "Camp.", "Gro.", "Mich.", "Oax.", "Q. Roo", "Tab.", "Chis."),
col = c(1, 3, 2, 1, 4, 3, 5, 2, 6, 5, 3, 6, 4, 4, 6, 3, 5, 7, 4, 5, 6, 5, 4, 6, 9, 8, 5, 4, 6, 9, 7, 7),
row = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8)
)
geofacet::grid_preview(mx_grid)
hom <- read_csv("https://elcri.men/data/victimas.csv.gz") %>%
filter(modalidad == "HOMICIDIOS" & tipo == "DOLOSOS") %>%
mutate(date = as.Date(str_c(date, "-01"), origin="1960-10-01")) %>%
mutate(rate = count / population * 10^5 * 12) %>%
mutate(code = as.integer(state_code))
ggplot(hom, aes(date, rate)) +
geom_line(color = "steelblue") +
facet_geo(~ code, grid = mx_grid, label = "name") +
ggtitle("Homicide rate in Mexico (Jan 2014 - May 2017)") +
ylab("annualized homicide rate") +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.