Your Document Title

Document Author

2025-02-26

Your Familiar Tools

Feel free to use the knitr infrastructure with dozens of tunable options in your document.

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(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
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(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(purrr)
library("htmlwidgets")
## Warning: package 'htmlwidgets' was built under R version 4.3.3
shapefile_path = 'BR_Municipios_2023.shp'
brasil_municipios <- st_read(shapefile_path)
## Reading layer `BR_Municipios_2023' from data source 
##   `C:\Users\lynco\Documents\R\BR_Municipios_2023.shp' using driver `ESRI Shapefile'
## Simple feature collection with 5572 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.99045 ymin: -33.75118 xmax: -28.84764 ymax: 5.271841
## Geodetic CRS:  SIRGAS 2000
dados1 = read_xlsx("artigo_casa_rui.xlsx", skip = 1, sheet = "mapa1") %>% 
  mutate(CodMun = as.character(CodMun))

dados2 = read_xlsx("artigo_casa_rui.xlsx", skip = 1, sheet = "mapa2") %>% 
  mutate(CodMun = as.character(CodMun))

dados3 = read_xlsx("artigo_casa_rui.xlsx", skip = 1, sheet = "mapa3") %>% 
  mutate(CodMun = as.character(CodMun))

dados4 = read_xlsx("artigo_casa_rui.xlsx", skip = 1, sheet = "mapa4") %>% 
  mutate(CodMun = as.character(CodMun))

dados <- list(dados1, dados2, dados3, dados4)

# Juntar todos os dataframes pela coluna CodMun
dados <- reduce(dados, left_join, by = "CodMun")

# Realizar o merge entre o shapefile e os dados do Excel pelo código do IBGE
brasil_municipios <- brasil_municipios %>%
  left_join(dados3 %>% mutate(CodMun = as.character(CodMun)), by = c("CD_MUN" = "CodMun"))

#brasil_municipios <- brasil_municipios %>%
#  filter(!is.na(meta_cv))
pal_programa <- colorFactor(palette = "Set3", domain = brasil_municipios$`nome do programa.x`)

# Calcular os percentis para a variável meta_cv
percentis_meta_cv <- quantile(brasil_municipios$meta_cv, probs = c(0, 0.2, 0.4, 0.6, 0.8, 1), na.rm = TRUE)

# Definir a paleta de cores com base nos percentis
pal_meta_cv <- colorBin(palette = "YlGnBu", domain = brasil_municipios$meta_cv, bins = percentis_meta_cv, na.color = "transparent")


mapa_brasil <- leaflet() %>%
  
  addPolygons(
    data = brasil_municipios %>%
      filter(!is.na(meta_cv)),
    fillColor = ~pal_meta_cv(meta_cv),
    weight = 1,
    opacity = 1,
    color = "black",
    fillOpacity = 0.7,
    popup = ~paste("Município: ", NM_MUN, "<br>Meta CV: ", `meta_cv`),
    group = "Meta CV"
  ) %>%
  
  addLegend(
    data = brasil_municipios %>%
      filter(!is.na(meta_cv)),
    pal = pal_meta_cv,
    values = ~meta_cv,
    opacity = 0.7,
    title = "Meta CV", 
    position = "bottomright",
    group = "Meta CV",
    labFormat = labelFormat(between = " - ")
  ) %>% 
  
  addLayersControl(
    overlayGroups = c("Nome do Programa", "Meta CV"),
    options = layersControlOptions(collapsed = FALSE)
  )
## Warning: sf layer has inconsistent datum (+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs).
## Need '+proj=longlat +datum=WGS84'
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ readr     2.1.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
brasil_municipios %>% 
  ggplot(aes(geometry = geometry)) + 
  geom_sf()