# Carregar pacotes
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(sf)
Warning: package 'sf' was built under R version 4.3.3
Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(dplyr)
Warning: package 'dplyr' was built under R version 4.3.3

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(tmap)
Warning: package 'tmap' was built under R version 4.3.3
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
library(geobr)
Warning: package 'geobr' was built under R version 4.3.3
library(readxl)
Warning: package 'readxl' was built under R version 4.3.3
library(janitor)
Warning: package 'janitor' was built under R version 4.3.3

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

    chisq.test, fisher.test
library(tidyr)
Warning: package 'tidyr' was built under R version 4.3.3
library(stringr)
Warning: package 'stringr' was built under R version 4.3.3
# Ler dados de IDH 1991
IDH_1991 <- read_excel("IDH_1991.xls") |> 
  clean_names() |> 
  mutate(codigo_do_municipio = as.character(codigo_do_municipio))

# Carregar os dados geoespaciais dos municípios da Paraíba (PB)
municipios <- read_municipality(code_muni = "PB", year = 2010) |> 
  clean_names() |> 
  mutate(code_muni = str_sub(code_muni, 1, 6))  #  6 dig
Using year/date 2010

Downloading: 2.7 kB     
Downloading: 2.7 kB     
Downloading: 13 kB     
Downloading: 13 kB     
Downloading: 46 kB     
Downloading: 46 kB     
Downloading: 70 kB     
Downloading: 70 kB     
Downloading: 86 kB     
Downloading: 86 kB     
Downloading: 110 kB     
Downloading: 110 kB     
Downloading: 130 kB     
Downloading: 130 kB     
Downloading: 190 kB     
Downloading: 190 kB     
Downloading: 220 kB     
Downloading: 220 kB     
Downloading: 220 kB     
Downloading: 220 kB     
# Unir dados de IDHM e geo
municipios_idhm <- municipios |> 
  left_join(IDH_1991, by = c("code_muni" = "codigo_do_municipio"))


# Configurar o modo estático para criação de mapas
tmap_mode("plot")
tmap mode set to plotting
# Criar o mapa para o IDHM de 1991 com intervalos personalizados
mapa_1991 <- tm_shape(municipios_idhm) +  # Usar o shape com a geometria fornecida
  tm_polygons("idhm_1991",
              breaks = c(0, 0.499, 0.599, 0.699, 0.799, 1),
              palette = c("red", "orange", "yellow", "green", "blue"),
              labels = c("Muito baixo", "Baixo", "Médio", "Alto", "Muito elevado"),
              title = "IDHM 1991") +  
  tm_layout(main.title = "IDHM dos Municípios em 1991")

# Criar o mapa para o IDHM de 2010 com as mesmas categorias para comparação
mapa_2010 <- tm_shape(municipios_idhm) +  # Usar o shape com a geometria fornecida
  tm_polygons("idhm_2010",
              breaks = c(0, 0.499, 0.599, 0.699, 0.799, 1),
              palette = c("red", "orange", "yellow", "green", "blue"),
              labels = c("Muito baixo", "Baixo", "Médio", "Alto", "Muito elevado"),
              title = "IDHM 2010") +  
  tm_layout(main.title = "IDHM dos Municípios em 2010")

# Mostrar os mapas lado a lado para comparação
tmap_arrange(mapa_1991, mapa_2010) |> print()