# Using brazilmaps

#### Renato Prado Siqueira

    library(brazilmaps)

## Local spatial objects

Every map returned by `get_brmap()` is installed with the package. No example in this vignette accesses the network.

    states <- get_brmap("state")
    states
    #> Simple feature collection with 27 features and 4 fields
    #> Geometry type: MULTIPOLYGON
    #> Dimension:     XY
    #> Bounding box:  xmin: -73.98681 ymin: -33.75077 xmax: -28.84784 ymax: 5.26891
    #> Geodetic CRS:  SIRGAS 2000
    #> First 10 features:
    #>    state_code      name state_abbreviation region_code
    #> 1          11  Rondônia                 RO           1
    #> 2          12      Acre                 AC           1
    #> 3          13  Amazonas                 AM           1
    #> 4          14   Roraima                 RR           1
    #> 5          15      Pará                 PA           1
    #> 6          16     Amapá                 AP           1
    #> 7          17 Tocantins                 TO           1
    #> 8          21  Maranhão                 MA           2
    #> 9          22     Piauí                 PI           2
    #> 10         23     Ceará                 CE           2
    #>                          geometry
    #> 1  MULTIPOLYGON (((-66.62689 -...
    #> 2  MULTIPOLYGON (((-73.79813 -...
    #> 3  MULTIPOLYGON (((-73.79813 -...
    #> 4  MULTIPOLYGON (((-63.37169 2...
    #> 5  MULTIPOLYGON (((-58.13787 -...
    #> 6  MULTIPOLYGON (((-54.87098 2...
    #> 7  MULTIPOLYGON (((-50.511 -12...
    #> 8  MULTIPOLYGON (((-48.75514 -...
    #> 9  MULTIPOLYGON (((-45.72347 -...
    #> 10 MULTIPOLYGON (((-41.32268 -...

The default municipal map is the newest installed edition:

    latest_year <- max(brmap_editions()$year)
    latest <- get_brmap("municipality")
    stopifnot(unique(latest$year) == latest_year)

## Filters use intersection

A named list can contain one or several constraints. Constraints are combined with AND, so the following result contains only municipalities that satisfy both conditions:

    ms <- get_brmap(
      "municipality",
      filters = list(region = 5, state = 50)
    )
    unique(ms[c("region_code", "state_code")])
    #> Simple feature collection with 79 features and 2 fields
    #> Geometry type: MULTIPOLYGON
    #> Dimension:     XY
    #> Bounding box:  xmin: -58.16865 ymin: -24.06838 xmax: -50.92289 ymax: -17.16634
    #> Geodetic CRS:  SIRGAS 2000
    #> First 10 features:
    #>      region_code state_code                       geometry
    #> 5104           5         50 MULTIPOLYGON (((-52.36427 -...
    #> 5105           5         50 MULTIPOLYGON (((-54.20297 -...
    #> 5106           5         50 MULTIPOLYGON (((-54.81266 -...
    #> 5107           5         50 MULTIPOLYGON (((-55.48613 -...
    #> 5108           5         50 MULTIPOLYGON (((-52.3919 -2...
    #> 5109           5         50 MULTIPOLYGON (((-53.58831 -...
    #> 5110           5         50 MULTIPOLYGON (((-55.76672 -...
    #> 5111           5         50 MULTIPOLYGON (((-51.03348 -...
    #> 5112           5         50 MULTIPOLYGON (((-55.58711 -...
    #> 5113           5         50 MULTIPOLYGON (((-55.07266 -...

## Joins and plots

    data("gini2015")

    plot_brmap(
      states,
      data = gini2015,
      by = c("state_code" = "cod"),
      fill_by = "gini"
    )

For full control, use the returned object directly with `ggplot2::geom_sf()`.

## Territorial hierarchy

    get_dtb(name = "Campo Grande")
    #>         code         name               level region_code  region_name
    #> 458   500001 Campo Grande    immediate_region           5 Centro-Oeste
    #> 629     5001 Campo Grande intermediate_region           5 Centro-Oeste
    #> 1290   50004 Campo Grande         microregion           5 Centro-Oeste
    #> 2426 2401305 Campo Grande        municipality           2     Nordeste
    #> 3004 2701506 Campo Grande        municipality           2     Nordeste
    #> 6461 5002704 Campo Grande        municipality           5 Centro-Oeste
    #>      state_code          state_name microregion_code microregion_name
    #> 458          50  Mato Grosso do Sul               NA             <NA>
    #> 629          50  Mato Grosso do Sul               NA             <NA>
    #> 1290         50  Mato Grosso do Sul            50004     Campo Grande
    #> 2426         24 Rio Grande do Norte            24003      Médio Oeste
    #> 3004         27             Alagoas            27006        Arapiraca
    #> 6461         50  Mato Grosso do Sul            50004     Campo Grande
    #>      mesoregion_code                    mesoregion_name immediate_region_code
    #> 458               NA                               <NA>                500001
    #> 629               NA                               <NA>                    NA
    #> 1290            5002 Centro Norte de Mato Grosso do Sul                    NA
    #> 2426            2401                     Oeste Potiguar                240009
    #> 3004            2702                   Agreste Alagoano                270007
    #> 6461            5002 Centro Norte de Mato Grosso do Sul                500001
    #>      immediate_region_name intermediate_region_code intermediate_region_name
    #> 458           Campo Grande                     5001             Campo Grande
    #> 629                   <NA>                     5001             Campo Grande
    #> 1290                  <NA>                       NA                     <NA>
    #> 2426               Mossoró                     2403                  Mossoró
    #> 3004             Arapiraca                     2702                Arapiraca
    #> 6461          Campo Grande                     5001             Campo Grande
    #>      municipality_code municipality_name
    #> 458                 NA              <NA>
    #> 629                 NA              <NA>
    #> 1290                NA              <NA>
    #> 2426           2401305      Campo Grande
    #> 3004           2701506      Campo Grande
    #> 6461           5002704      Campo Grande
    get_dtb_levels(
      c("municipality", "immediate_region"),
      filters = list(state = 50)
    )
    #>      municipality_code        municipality_name immediate_region_code
    #> 6442           5000203               Água Clara                500002
    #> 6443           5000252              Alcinópolis                500004
    #> 6444           5000609                  Amambai                500009
    #> 6445           5000708                Anastácio                500012
    #> 6446           5000807             Anaurilândia                500007
    #> 6447           5000856                 Angélica                500007
    #> 6448           5000906             Antônio João                500008
    #> 6449           5001003     Aparecida do Taboado                500003
    #> 6450           5001102               Aquidauana                500012
    #> 6451           5001243             Aral Moreira                500008
    #> 6452           5001508             Bandeirantes                500001
    #> 6453           5001904               Bataguassu                500002
    #> 6454           5002001                Batayporã                500007
    #> 6455           5002100               Bela Vista                500011
    #> 6456           5002159                Bodoquena                500012
    #> 6457           5002209                   Bonito                500011
    #> 6458           5002308              Brasilândia                500002
    #> 6459           5002407                  Caarapó                500005
    #> 6460           5002605                  Camapuã                500001
    #> 6461           5002704             Campo Grande                500001
    #> 6462           5002803                  Caracol                500011
    #> 6463           5002902              Cassilândia                500003
    #> 6464           5002951          Chapadão do Sul                500003
    #> 6465           5003108                Corguinho                500001
    #> 6466           5003157         Coronel Sapucaia                500009
    #> 6467           5003207                  Corumbá                500010
    #> 6468           5003256               Costa Rica                500004
    #> 6469           5003306                    Coxim                500004
    #> 6470           5003454               Deodápolis                500005
    #> 6471           5003488    Dois Irmãos do Buriti                500001
    #> 6472           5003504                Douradina                500005
    #> 6473           5003702                 Dourados                500005
    #> 6474           5003751                 Eldorado                500006
    #> 6475           5003801            Fátima do Sul                500005
    #> 6476           5003900                Figueirão                500004
    #> 6477           5004007       Glória de Dourados                500005
    #> 6478           5004106     Guia Lopes da Laguna                500011
    #> 6479           5004304                 Iguatemi                500006
    #> 6480           5004403                Inocência                500003
    #> 6481           5004502                  Itaporã                500005
    #> 6482           5004601                Itaquiraí                500006
    #> 6483           5004700                 Ivinhema                500007
    #> 6484           5004809                   Japorã                500006
    #> 6485           5004908                Jaraguari                500001
    #> 6486           5005004                   Jardim                500011
    #> 6487           5005103                    Jateí                500005
    #> 6488           5005152                     Juti                500005
    #> 6489           5005202                  Ladário                500010
    #> 6490           5005251            Laguna Carapã                500005
    #> 6491           5005400                 Maracaju                500005
    #> 6492           5005608                  Miranda                500012
    #> 6493           5005681               Mundo Novo                500006
    #> 6494           5005707                  Naviraí                500006
    #> 6495           5005806                  Nioaque                500011
    #> 6496           5006002     Nova Alvorada do Sul                500001
    #> 6497           5006200           Nova Andradina                500007
    #> 6498           5006259    Novo Horizonte do Sul                500007
    #> 6499           5006275        Paraíso das Águas                500003
    #> 6500           5006309                Paranaíba                500003
    #> 6501           5006358                 Paranhos                500009
    #> 6502           5006408              Pedro Gomes                500004
    #> 6503           5006606               Ponta Porã                500008
    #> 6504           5006903           Porto Murtinho                500011
    #> 6505           5007109       Ribas do Rio Pardo                500001
    #> 6506           5007208            Rio Brilhante                500005
    #> 6507           5007307                Rio Negro                500001
    #> 6508           5007406 Rio Verde de Mato Grosso                500004
    #> 6509           5007505                  Rochedo                500001
    #> 6510           5007554      Santa Rita do Pardo                500002
    #> 6511           5007695     São Gabriel do Oeste                500001
    #> 6512           5007703              Sete Quedas                500009
    #> 6513           5007802                 Selvíria                500002
    #> 6514           5007901              Sidrolândia                500001
    #> 6515           5007935                   Sonora                500004
    #> 6516           5007950                   Tacuru                500009
    #> 6517           5007976               Taquarussu                500007
    #> 6518           5008008                  Terenos                500001
    #> 6519           5008305              Três Lagoas                500002
    #> 6520           5008404                Vicentina                500005
    #>                          immediate_region_name
    #> 6442                               Três Lagoas
    #> 6443                                     Coxim
    #> 6444                                   Amambai
    #> 6445                    Aquidauana - Anastácio
    #> 6446                            Nova Andradina
    #> 6447                            Nova Andradina
    #> 6448                                Ponta Porã
    #> 6449 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6450                    Aquidauana - Anastácio
    #> 6451                                Ponta Porã
    #> 6452                              Campo Grande
    #> 6453                               Três Lagoas
    #> 6454                            Nova Andradina
    #> 6455                                    Jardim
    #> 6456                    Aquidauana - Anastácio
    #> 6457                                    Jardim
    #> 6458                               Três Lagoas
    #> 6459                                  Dourados
    #> 6460                              Campo Grande
    #> 6461                              Campo Grande
    #> 6462                                    Jardim
    #> 6463 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6464 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6465                              Campo Grande
    #> 6466                                   Amambai
    #> 6467                                   Corumbá
    #> 6468                                     Coxim
    #> 6469                                     Coxim
    #> 6470                                  Dourados
    #> 6471                              Campo Grande
    #> 6472                                  Dourados
    #> 6473                                  Dourados
    #> 6474                      Naviraí - Mundo Novo
    #> 6475                                  Dourados
    #> 6476                                     Coxim
    #> 6477                                  Dourados
    #> 6478                                    Jardim
    #> 6479                      Naviraí - Mundo Novo
    #> 6480 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6481                                  Dourados
    #> 6482                      Naviraí - Mundo Novo
    #> 6483                            Nova Andradina
    #> 6484                      Naviraí - Mundo Novo
    #> 6485                              Campo Grande
    #> 6486                                    Jardim
    #> 6487                                  Dourados
    #> 6488                                  Dourados
    #> 6489                                   Corumbá
    #> 6490                                  Dourados
    #> 6491                                  Dourados
    #> 6492                    Aquidauana - Anastácio
    #> 6493                      Naviraí - Mundo Novo
    #> 6494                      Naviraí - Mundo Novo
    #> 6495                                    Jardim
    #> 6496                              Campo Grande
    #> 6497                            Nova Andradina
    #> 6498                            Nova Andradina
    #> 6499 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6500 Paranaíba - Chapadão do Sul - Cassilândia
    #> 6501                                   Amambai
    #> 6502                                     Coxim
    #> 6503                                Ponta Porã
    #> 6504                                    Jardim
    #> 6505                              Campo Grande
    #> 6506                                  Dourados
    #> 6507                              Campo Grande
    #> 6508                                     Coxim
    #> 6509                              Campo Grande
    #> 6510                               Três Lagoas
    #> 6511                              Campo Grande
    #> 6512                                   Amambai
    #> 6513                               Três Lagoas
    #> 6514                              Campo Grande
    #> 6515                                     Coxim
    #> 6516                                   Amambai
    #> 6517                            Nova Andradina
    #> 6518                              Campo Grande
    #> 6519                               Três Lagoas
    #> 6520                                  Dourados

`microregion` and `mesoregion` are retained as discontinued historical IBGE divisions. `immediate_region` and `intermediate_region` are the current regional divisions.

Legacy `sp` output remains available with `output = "sp"` when package `sp` is installed. New code should use the default `sf` output.