Leaflet map with R shows country completely grey

See the R leaflet documentation page for plotting Choropleth Maps:

https://rstudio.github.io/leaflet/choropleths.html

You need to specify a color palette you want to use for the map. The following code produces the output below.

library(dplyr)
library(leaflet)
library(rgdal)
library(sf)

# Import data 
load('C:/[...]/data_19.rda') # <--- There are several variables, I selected just one of them

# Import Italy shapefile
ita_map = readOGR('C:/[...]/Limiti01012020_g/ProvCM01012020_g', 
                  'ProvCM01012020_g_WGS84', stringsAsFactors = FALSE)

# Merge
colnames(data_19)[1] = "COD_PROV" # <--- This is the numerical provincial code
ita_map_sf = st_as_sf(ita_map)
ita_map_sf$COD_PROV <- as.numeric(ita_map_sf$COD_PROV)
ita_map_data <- left_join(ita_map_sf, data_19, by = "COD_PROV")

# Specify Color Palette
pal <- colorQuantile("Blues", domain = ita_map_data$unilav_ula, n=5)

# Generate map with leaflet
ita_map_data %>%
  st_transform(crs = 4326) %>%
  leaflet() %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>%
  setView(lat = 41.8719, lng = 12.5674, zoom = 5) %>%
  addPolygons(
    fillColor = ~pal(unilav_ula),
    stroke = FALSE,
    smoothFactor = 0.2,
    fillOpacity = 0.7,
    label=~unilav_ula
  )

Output map

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top