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
and df_mxmunicipio
, are provided with demographic variables from the Encuesta Intercensal 2015.
library(mxmaps)
library(htmlTable)
data("df_mxstate")
htmlTable(head(df_mxstate), rnames = FALSE, col.rgroup = c("none", "#F7F7F7"))
region | state_name | state_name_official | state_abbr | state_abbr_official | pop | pop_male | pop_female | afromexican | part_afromexican | indigenous | part_indigenous |
---|---|---|---|---|---|---|---|---|---|---|---|
01 | Aguascalientes | Aguascalientes | AGS | Ags. | 1312544 | 640091 | 672453 | 653 | 4559 | 153395 | 18716 |
02 | Baja California | Baja California | BC | BC | 3315766 | 1650341 | 1665425 | 7445 | 10432 | 283055 | 38391 |
03 | Baja California Sur | Baja California Sur | BCS | BCS | 712029 | 359137 | 352892 | 11032 | 5132 | 103034 | 11728 |
04 | Campeche | Campeche | CAMP | Camp. | 899931 | 441276 | 458655 | 3554 | 6833 | 400811 | 13140 |
05 | Coahuila | Coahuila de Zaragoza | COAH | Coah. | 2954915 | 1462612 | 1492303 | 2761 | 8137 | 204890 | 28588 |
06 | Colima | Colima | COL | Col. | 711235 | 350791 | 360444 | 762 | 3314 | 145297 | 12373 |
data("df_mxmunicipio")
htmlTable(head(df_mxmunicipio), rnames = FALSE, col.rgroup = c("none", "#F7F7F7"))
state_code | municipio_code | region | state_name | state_name_official | state_abbr | state_abbr_official | municipio_name | pop | pop_male | pop_female | afromexican | part_afromexican | indigenous | part_indigenous | metro_area | long | lat |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
01 | 001 | 01001 | Aguascalientes | Aguascalientes | AGS | Ags. | Aguascalientes | 877190 | 425731 | 451459 | 532 | 2791 | 104125 | 14209 | Aguascalientes | -102.296047 | 21.879823 |
01 | 002 | 01002 | Aguascalientes | Aguascalientes | AGS | Ags. | Asientos | 46464 | 22745 | 23719 | 3 | 130 | 1691 | 92 | -102.089275 | 22.238317 | |
01 | 003 | 01003 | Aguascalientes | Aguascalientes | AGS | Ags. | Calvillo | 56048 | 27298 | 28750 | 10 | 167 | 7358 | 2223 | -102.718751 | 21.846907 | |
01 | 004 | 01004 | Aguascalientes | Aguascalientes | AGS | Ags. | Cosío | 15577 | 7552 | 8025 | 0 | 67 | 2213 | 191 | -102.300044 | 22.366409 | |
01 | 005 | 01005 | Aguascalientes | Aguascalientes | AGS | Ags. | Jesús María | 120405 | 60135 | 60270 | 32 | 219 | 8679 | 649 | Aguascalientes | -102.343416 | 21.961273 |
01 | 006 | 01006 | Aguascalientes | Aguascalientes | AGS | Ags. | Pabellón de Arteaga | 46473 | 22490 | 23983 | 3 | 74 | 6232 | 251 | -102.276542 | 22.149202 |
State names and abbreviations come in two styles since normal people always use Veracruz instead of the full official name of Veracruz de Ignacio de la Llave, and it's kind of awkward to use the Chiapas official abbreviation of Chis.
library("geojsonio")
# Convert the topoJSON to spatial object
data(mxstate.topoJSON)
tmpdir <- tempdir()
# have to use RJSONIO or else the topojson isn't valid
write(RJSONIO::toJSON(mxstate.topoJSON), file.path(tmpdir, "state.topojson"))
states <- topojson_read(file.path(tmpdir, "state.topojson"))
## Reading layer `state' from data source `/tmp/diego/RtmptTSrTw/state.topojson' using driver `TopoJSON'
## Simple feature collection with 32 features and 1 field
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -117.124 ymin: 14.5321 xmax: -86.71842 ymax: 32.71863
## CRS: NA
str_mxstate(c(32, 5))
## [1] "32" "05"
str_mxmunicipio(c(32, 5), c(9, 18))
## [1] "32009" "05018"
str_mxmunicipio(c(32001, 5009))
## [1] "32001" "05009"