Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/diegovalle/mxmaps

An R package for making maps of Mexico
https://github.com/diegovalle/mxmaps

map mexico r r-package rstats

Last synced: about 1 month ago
JSON representation

An R package for making maps of Mexico

Awesome Lists containing this project

README

        

---
title: "Mexico Choropleths"
author: "Diego Valle-Jones"
date: "`r format(Sys.Date(), '%B %d, %Y')`"
output:
github_document:
toc: true
fig_width: 8
fig_height: 5
---

Master: [![R build status](https://github.com/diegovalle/mxmaps/workflows/R-CMD-check/badge.svg)](https://github.com/diegovalle/mxmaps/actions)
[![Coverage Status](https://coveralls.io/repos/github/diegovalle/mxmaps/badge.svg?branch=master)](https://coveralls.io/github/diegovalle/mxmaps?branch=master)

| | |
|--------------|--------------------------------------------------------------------------|
| __Author:__ | Diego Valle-Jones |
| __License:__ | [BSD_3](https://opensource.org/licenses/BSD-3-Clause) |
| __Website:__ | [https://www.diegovalle.net/mxmaps/](https://www.diegovalle.net/mxmaps/) |
| __Forum:__ | [Google Group](https://groups.google.com/forum/#!forum/mxmaps) |

## What does it do?

This package is based on [choroplethr](https://CRAN.R-project.org/package=choroplethr) and can be used to easily create maps of Mexico at both the state and municipio levels. It also includes functions to create interactive maps using the leaflet package, map INEGI data from its [API](https://CRAN.R-project.org/package=inegiR), and format strings so they match the INEGI state and municipio codes. Be sure to visit the [official website](https://www.diegovalle.net/mxmaps/).

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.path = "man/figures/")
```

## Installation

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

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

## Quick Example

```{r}
library(mxmaps)

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

## Data

The data.frame that you provide to the plotting functions must have one column named "region" and one column named "value". The entries for "region" must match the INEGI codes for states ("01", "02", etc) and municipios ("01001", "01002", etc) either as a string with or without a leading "0" or as numerics. The functions `str_mxstate` and `str_mxmunicipio` are provided to easily format codes to the INEGI specification. Also, two example data.frames, `df_mxstate_2020` and `df_mxmunicipio_2020`, are provided with demographic variables from the 2020 census.

```{r}
data("df_mxstate_2020")
knitr::kable(head(df_mxstate_2020))
data("df_mxmunicipio_2020")
knitr::kable(head(df_mxmunicipio_2020))
```

## Municipios

Here's another example showing Mexican municipios (similar to counties):

```{r}
data("df_mxmunicipio_2020")
df_mxmunicipio_2020$value <- df_mxmunicipio_2020$indigenous_language / df_mxmunicipio_2020$pop
mxmunicipio_choropleth(df_mxmunicipio_2020, num_colors = 1,
title = "Percentage of the population that speaks\nan indigenous language")

```