PIB por Unidade Federativa do BR

Author

Sabrina Araujo

Objetivo

O objetivo da atividade é obter uma série de dados do SIDRA/IBGE por Unidade da Federação (UF) e harmonizar a uma base espacial do pacote geobr.

Pacotes utilizados

library(sidrar)
library(geobr)
Loading required namespace: sf
library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(janitor)

Attaching package: 'janitor'
The following objects are masked from 'package:stats':

    chisq.test, fisher.test

Obtenção e tratamento dos dados

pib_uf <- get_sidra(
  api = "/t/5938/n3/all/v/37/p/2021"
)
All others arguments are desconsidered when 'api' is informed
pib_uf <- pib_uf %>%
  clean_names() %>%
  select(
    uf_codigo = unidade_da_federacao_codigo,
    uf_nome = unidade_da_federacao,
    pib = valor
  ) %>%
  mutate(
    uf_codigo = as.numeric(uf_codigo)
  )

head(pib_uf)
  uf_codigo  uf_nome       pib
2        11 Rondônia  58170096
3        12     Acre  21374440
4        13 Amazonas 131531038
5        14  Roraima  18202579
6        15     Pará 262904979
7        16    Amapá  20099851
library(DT) 
datatable( pib_uf, options = list( pageLength = 10, autoWidth = TRUE ), caption = "PIB por Unidade da Federação - SIDRA 2021" )

Leitura da malha geográfica

mapa_uf <- read_state(year = 2019)
Using year/date 2019
mapa_uf <- mapa_uf %>%
  mutate(
    uf_codigo = as.numeric(code_state)
  )

head(mapa_uf)
Simple feature collection with 6 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -73.99045 ymin: -13.6937 xmax: -46.06151 ymax: 5.271841
Geodetic CRS:  SIRGAS 2000
  code_state abbrev_state name_state code_region name_region
1         11           RO   Rondônia           1       Norte
2         12           AC       Acre           1       Norte
3         13           AM   Amazônas           1       Norte
4         14           RR    Roraima           1       Norte
5         15           PA       Pará           1       Norte
6         16           AP      Amapá           1       Norte
                            geom uf_codigo
1 MULTIPOLYGON (((-65.3815 -1...        11
2 MULTIPOLYGON (((-71.07772 -...        12
3 MULTIPOLYGON (((-69.83766 -...        13
4 MULTIPOLYGON (((-63.96008 2...        14
5 MULTIPOLYGON (((-51.43248 -...        15
6 MULTIPOLYGON (((-50.45011 2...        16

Junção dos dados e construção do mapa

mapa_final <- left_join(
  mapa_uf,
  pib_uf,
  by = "uf_codigo"
)

ggplot(mapa_final) +
  geom_sf(aes(fill = pib), color = "white") +
  scale_fill_viridis_c(
    option = "viridis",
    name = "PIB"
  ) +
  labs(
    title = "PIB por Unidade da Federação - 2021",
    subtitle = "Dados do SIDRA/IBGE",
    caption = "Fonte: SIDRA e geobr"
  ) +
  theme_minimal()


Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).