Recommand · October 14, 2021 0

How to color sections of a shape file in a leaflet map

I currently have a map, with 2 shape files, and I would like to specify and color individual departments of the country’s shape files that I have loaded, by specifying the code in the shape files’ attribute table.

Can someone help me?

library(leaflet)
library(sf)
d1<-read.csv(file.choose())

nombrecolores1<-c('Honduras','El Salvador',unique(d1$Tipo))
colores1<-c('#000000','blue','#096922','red')
pal<-colorFactor(colores1,domain = nombrecolores1)
palHN<-colorQuantile("YlOrRd", NULL, n = 18)
shapeHN  <- st_read('D:/Works/Fernando/Coding/R/azure_maps/Shapes/hnd_admbnda_adm0_sinit_20161005.shp')
shapeSV  <- st_read('D:/Works/Fernando/Coding/R/azure_maps/Shapes/slv_admbnda_adm0_gadm_20210204.shp')

m<-leaflet() %>%
  addTiles()%>%
  addTerminator(resolution = 10)%>%
  setView(lat=14.0818,lng=-87.20681,zoom=6)
esri <- grep("^Esri", providers, value = TRUE)
  m <- m %>%
    addProviderTiles(providers$Esri.NatGeoWorldMap)%>%
    addCircles( lat = d1$Latitud, lng = d1$Longitud,
                color = pal(nombrecolores1),
                        weight = 3, radius=4,popup=paste("<strong>Sistema: </strong>",d1$Sistema,"<br>",
                                                          "<strong>Ranking General: </strong>", d1$Ranking, "<br>"),
  )%>%addLegend(position = 'bottomright',pal = pal,values = nombrecolores1, title = paste('CRS AZURE')) %>%
    addPolygons(data=shapeHN,weight=3,col = '#096922') %>%
    addPolygons(data=shapeSV,weight=3,col = 'blue')

m