This R package can be used to easily create maps of Mexico at both the state and municipio levels. It is based on the choroplethr package and includes functions to create interactive maps using the leaflet package, map INEGI data using their API with the inegiR package, and format strings so they match the INEGI state and municipio codes.

## Installation

For the moment this package is only available from github. For the development version:

if (!require("devtools")) {
install.packages("devtools")
}
devtools::install_github("diegovalle/mxmaps")

mxmaps depends on the sf package which has other dependencies

## Quick Example

library("mxmaps")

df_mxstate$value <- df_mxstate$pop
mxstate_choropleth(df_mxstate,
title = "Total population, by state") 

What the data looks like:

knitr::kable(head(df_mxstate[,c("region", "value")]))
region value
01 1312544
02 3315766
03 712029
04 899931
05 2954915
06 711235

## Basic Usage

The two basic functions of the package are mxstate_choropleth and mxmunicipio_choropleth. The data.frame that you provide to the plotting functions must have one column named region with the INEGI codes of the states or municipios and one column named value with the values to plot. You can specify a title with the title parameter and the number of buckets with the num_color parameter. If you want a continuous scale you can set the num_colors parameter equal to 1.

library("mxmaps")

df_mxstate$value <- df_mxstate$indigenous / df_mxstate$pop * 100 mxstate_choropleth(df_mxstate, num_colors = 1, title = "Percentage of the population that self-identifies\nas indigenous", legend = "%") ## Municipios You can also plot Mexican municipios (similar to counties) with a continuous color scale set with num_colors = 1 data("df_mxmunicipio") df_mxmunicipio$value <-  df_mxmunicipio$indigenous / df_mxmunicipio$pop * 100
mxmunicipio_choropleth(df_mxmunicipio,
num_colors = 1,
title = "Percentage of the population that self-identifies\nas indigenous",
legend = "%")

You can also subset the area to show in the choropleth by using the zoom parameter:

mxmunicipio_choropleth(df_mxmunicipio, num_colors = 1,
zoom = subset(df_mxmunicipio, metro_area %in% c("Valle de México",
"Puebla-Tlaxcala",
"Cuernavaca",
"Toluca"))\$region,
title = "Percentage of the population that self-identifies\nas indigenous",
legend = "%") 

You can use the show_states parameter to hide or show the state borders when making municipio choropleths.

mxmunicipio_choropleth(df_mxmunicipio, num_colors = 1,
zoom = c(31001:31106,30002:30212),
title = "Percentage of the population that self-identifies\nas indigenous in Yucatán and Veracruz",
show_states = FALSE,
legend = "%")