Leaflet Estados

#include=F para nao mostrar. colocando somente para o RPUBS
library(leaflet)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(htmltools)
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(stats)

# leaflet()  %>%  addTiles()
taxa_homic = read.csv2("C:/Users/B14578742709/Downloads/taxa_2020_uf.csv",encoding = "UTF-8")
taxa_homic$uf = c("Acre","Alagoas","Amapá","Amazonas","Bahia",
                  "Ceará","Distrito Federal","Espírito Santo","Goiás","Maranhão", 
                  "Mato Grosso","Mato Grosso do Sul","Minas Gerais","Pará","Paraíba"  , 
                  "Paraná","Pernambuco","Piauí","Rio de Janeiro","Rio Grande do Norte",
                  "Rio Grande do Sul","Rondônia","Roraima","Santa Catarina","São Paulo",
                  "Sergipe","Tocantins" )
shp_estados = st_read("C:/Users/B14578742709/Desktop/Atlas/shape_files/estados_2010/estados_2010.shp")
## Reading layer `estados_2010' from data source 
##   `C:\Users\B14578742709\Desktop\Atlas\shape_files\estados_2010\estados_2010.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 27 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.99024 ymin: -33.75136 xmax: -32.39088 ymax: 5.270972
## Geodetic CRS:  WGS 84
shp_municipios = st_read("C:/Users/B14578742709/Desktop/Atlas/shape_files/municipios_2010/municipios_2010.shp")
## Reading layer `municipios_2010' from data source 
##   `C:\Users\B14578742709\Desktop\Atlas\shape_files\municipios_2010\municipios_2010.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 5564 features and 7 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -73.99094 ymin: -33.75158 xmax: -32.39219 ymax: 5.272156
## Geodetic CRS:  WGS 84
shp_estados = merge(shp_estados,taxa_homic,
                    by.x = "nome",
                    by.y = "uf",
                    all.x = T)
# palhetas: https://r-graph-gallery.com/38-rcolorbrewers-palettes.html
taxa_cor <- colorQuantile("Set1", shp_estados$taxa_2020, n = 6)
taxa_colors <- unique(taxa_cor(sort(shp_estados$taxa_2020))) # hex codes
taxa_labs <- quantile(shp_estados$taxa_2020, 
                      attr(taxa_cor,"colorArgs")$probs) 
taxa_labs <- paste(round(lag(taxa_labs),1), round(taxa_labs,1), sep = " - ")[-1]

mapa = leaflet() %>% 
  addTiles() %>% 
  addPolygons(data = shp_estados, 
              stroke = F, 
              color = ~taxa_cor(taxa_2020),
              fillOpacity = 0.6,
              label = lapply(paste(shp_estados$sigla, " - ", shp_estados$nome, "<br>",
                                   "Taxa de homicídios: ", shp_estados$taxa_2020, "<br>"),
              HTML),
              # smoothFactor = 0.2,
              highlight = highlightOptions(weight = 2,
                                           color = "black",
                                           # bringToFront = TRUE,
                                           stroke = T),
              popup = paste(shp_estados$sigla, " - ", shp_estados$nome, "<br>",
                            "Taxa de homicídios: ", shp_estados$taxa_2020, "<br>")) %>%
  # Mapa municipios
  # addPolygons(data = shp_municipios# %>% filter(uf == "BA")
  #             ,
  #             fillColor = 0.1,
  #             weight = 0.5,
  #             fillOpacity = 0.1,
  #             color = "black") %>%
  # Legenda - > nao precisa pq adicionei o label em addPolygons, que ai eh 
  # so passar o mouse por cima
  addLegend(colors = taxa_colors, 
            labels = taxa_labs,
            opacity = 0.75,
            title = "Taxa de homicídios",
            position = "bottomright")