library(sf) library(leaflet) library(dplyr) library(classInt)
shapefile_path <- “C:/Users/oeurtubiav/OneDrive - Instituto Nacional de Estadisticas/Documentos/OEURTUBIAV/COBERTURA/API DISEMINACIÓN GEO/SHAPES/POB_CENSO_2024.shp” comunas <- st_read(shapefile_path)
required_fields <- c(“COMUNA_1”, “POB_31_07”, “Índice_de”, “REGION”) if (!all(required_fields %in% colnames(comunas))) { stop(“El shapefile debe contener las columnas: COMUNA_1, POB_31_07, Índice_de, REGION.”) }
comunas <- comunas %>% mutate( popup_info = paste0( “Comuna:”, COMUNA_1, “
”, “Población (31-07):”, POB_31_07, “
”, “Índice:”, Índice_de ) )
breaks <- classIntervals(comunas\(POB_31_07, n = 5, style = "jenks")\)brks
comunas <- comunas %>% mutate( indice_categoria = cut( POB_31_07, breaks = breaks, include.lowest = TRUE, labels = c(“Muy Bajo”, “Bajo”, “Medio”, “Alto”, “Muy Alto”) ) )
paleta_colores <- colorFactor( palette = “YlOrRd”, domain = comunas$indice_categoria )
regiones <- unique(comunas$REGION)
selector_html <- paste0( “” )
cat(“Seleccionar Región:”, selector_html)
cat(”“)
mapa <- leaflet(data = comunas) %>%
addProviderTiles(providers$Esri.WorldImagery, group = “Google Earth”) %>%
addPolygons( fillColor = ~paleta_colores(indice_categoria), color = “white”, weight = 1, fillOpacity = 0.7, highlightOptions = highlightOptions( weight = 2, color = “#666”, fillOpacity = 0.9, bringToFront = TRUE ), popup = ~popup_info ) %>%
addLegend( pal = paleta_colores, values = ~indice_categoria, title = “Índice de Categorías”, position = “bottomright” ) %>%
setMaxBounds( lng1 = -75, lat1 = -56, # Extremo suroeste lng2 = -66, lat2 = -17 # Extremo noreste )
mapa