map_comuni_leaflet.RdCreates an interactive web map of Italian municipalities using Leaflet.
This function demonstrates how to visualize the geographic data prepared
by prepare_comuni_maps.
map_comuni_leaflet(
comuni_sf,
color_by = "COD_REG",
palette = "Set3",
popup_fields = c("COMUNE", "DEN_REG", "DEN_UTS", "PRO_COM"),
tile_provider = "OpenStreetMap",
add_legend = TRUE
)An sf object with municipality boundaries and attributes.
Can be loaded from RDS file created by prepare_comuni_maps
or read directly from GeoJSON.
Character string. Attribute name to use for coloring polygons. Common options: "COD_REG" (by region), "COD_UTS" (by province). Set to NULL for uniform color. Default is "COD_REG".
Character string. Color palette name from RColorBrewer.
Defaults to "Set3". See RColorBrewer::display.brewer.all() for options.
Character vector. Fields to display in popup when clicking a municipality. Defaults to c("COMUNE", "DEN_REG", "DEN_UTS", "PRO_COM").
Character string. Base map tile provider. Options: "OpenStreetMap" (default), "CartoDB.Positron", "Esri.WorldImagery".
Logical. Add a legend for colored regions. Default TRUE. Only applicable when color_by is not NULL.
A leaflet map object that can be displayed interactively or embedded in Shiny applications.
This function is now a wrapper around map_territorial_units.
For more flexibility with different territorial units (provinces, regions, etc.),
consider using map_territorial_units directly.
if (FALSE) { # \dontrun{
# Prepare maps
files <- prepare_comuni_maps()
# Load the data
comuni <- readRDS(files$rds)
# Basic map colored by region
map_comuni_leaflet(comuni)
# Map colored by province
map_comuni_leaflet(comuni, color_by = "COD_UTS")
# Uniform color, minimal popup
map_comuni_leaflet(
comuni,
color_by = NULL,
popup_fields = "COMUNE"
)
# Use satellite imagery
map_comuni_leaflet(
comuni,
tile_provider = "Esri.WorldImagery"
)
# In a Shiny app
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("map", height = "600px")
)
server <- function(input, output) {
comuni <- readRDS("data/comuni_map_20250114.rds")
output$map <- renderLeaflet({
map_comuni_leaflet(comuni)
})
}
shinyApp(ui, server)
} # }