knitr::opts_chunk$set(echo = TRUE, 
                      fig.dim = c(8, 5), 
                      dpi = 200, 
                      out.width = '100%')

mxmaps - create maps of Mexico

R build status Coverage Status lifecycle CRAN_Status_Badge

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. Feel free to ask for help in the discussion forum

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_2020$value <- df_mxstate_2020$pop
mxstate_choropleth(df_mxstate_2020,
                   title = "Total population, by state") 

What the data looks like:

knitr::kable(head(df_mxstate_2020[,c("region", "value")]))

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 another one named value with the values to plot. You can specify a title with the title parameter and the number of buckets for the color scale with the num_color parameter. If you need a continuous scale you can set the num_colors parameter equal to 1.

library("mxmaps")

df_mxstate_2020$value <-  df_mxstate_2020$indigenous_language / 
  df_mxstate_2020$pop * 100
mxstate_choropleth(df_mxstate_2020, 
                   num_colors = 1,
                   title = "Percentage of the population that speaks\nan indigenous language",
                   legend = "%")

Municipios

You can also plot Mexican municipios (similar to counties) with a continuous color scale set with num_colors = 1

data("df_mxmunicipio_2020")
df_mxmunicipio_2020$value <-  df_mxmunicipio_2020$indigenous_language / 
  df_mxmunicipio_2020$pop * 100
mxmunicipio_choropleth(df_mxmunicipio_2020, 
                       num_colors = 1,
                       title = "Percentage of the population that speaks\nan indigenous language",
                       legend = "%")

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

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

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

mxmunicipio_choropleth(df_mxmunicipio_2020, num_colors = 1,
                       zoom = subset(df_mxmunicipio_2020, state_name %in% 
                                       c("Yucatán", "Veracruz"))$region,
                       title = "Percentage of the population that speaks\nan indigenous language in Yucatán and Veracruz",
                       show_states = FALSE,
                       legend = "%")


diegovalle/mxmaps documentation built on Sept. 22, 2023, 9:57 p.m.