I am wondering if there are any methods to reduce the size of leaflet maps. Generating a bare-bones map of the U.S. with only the county polygons produces a 23 MB map. And when I add my data and states, I end up with one that is 30-40 MB.
For using these on the web, that size is simply way too big for people with poor connections. The loading time is unacceptable.
As far as functionality for my current use case, I do need to be able to interact with the counties - click on them and see a popup with information.
Is there any way to reduce the file size of a leaflet map? Changing the resolution? Using some different layer other than polygons? Would a raster layer reduce the size while maintaining the same functionality?
EDIT: I've changed the tigris counties call to counties(cb = T, resolution = "20m")
. Obviously it doesn't look as nice, but it does reduce the size down to 13.3 Mb. Still, I'm looking for additional solutions.
library(tidyverse)
library(leaflet)
library(tigris)
library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
county_sf <- counties(cb = T) %>%
shift_geometry(geoid_column = "GEOID") %>%
st_transform("EPSG:4326")
county_map <- county_sf %>%
leaflet() %>%
addPolygons(weight = 0.5, color = "#000000", opacity = 1,
group = "counties") %>%
setView(zoom = 3.5, lat = 38, lng = -100)
print(object.size(county_map), units = "auto")
#> 39.7 Mb