Leaflet - Cluster Option and Base Map Issue

Hi,

I have been having a problem for two days that the markerClusterOptions() is not working. I can't figure out why :sweat: . When I run the code without the markerClusterOptions(), it shows the markers, but without a base map. In both cases, the base map is not shown. Any idea what is going on will be much appreciated.

Thank you


#Education Mapping 

#Library ----
library(dplyr)
library(leaflet)

#Load -----
Clergy_edu <- readr::read_csv("ClergyEdu_Geo.csv")

#Clean Coordinates ----
Clergy_edu <- Clergy_edu[!is.na(Clergy_edu$lat), ]
Clergy_edu <- Clergy_edu[!duplicated(Clergy_edu$lat), ]
Clergy_edu <- Clergy_edu[!is.na(Clergy_edu$postal_code), ]
Clergy_edu <- Clergy_edu[!is.na(Clergy_edu$geo_diocese), ]
Clergy_edu <- Clergy_edu[!is.na(Clergy_edu$state), ]
non_continental <- c("AB","AK","HI","PR")
Clergy_edu <- Clergy_edu[!(Clergy_edu$state %in% non_continental), ]

data <- Clergy_edu %>%
  select(
    lon,
    lat,
    client_nbr,
    first_name,
    last_name,
    marital_status,
    geo_diocese,
    geo_province,
    state,
    postal_code,
    Address_Geo,
    email_address)



#Transform to Spatial Point DataFrame ----

coords <- Clergy_edu %>%
          select(
            lon,
            lat)


crs    <- CRS("+init=epsg:4326")


spdf <- SpatialPointsDataFrame(coords = coords,
                               data = data,
                               proj4string = crs)

tmap::qtm(spdf)

#Leaflet Mapping ----


lf_clergy       <- leaflet() %>%
                  addMarkers(spdf,
                            lng =   spdf$lon,
                            lat =   spdf$lat,
                            markerClusterOptions())

lf_clergy

I don't have access to your data, so I am shooting blind.

However, when I consider your leaflet call like this (see below) I don't see any basemap being added. So a display with no basemap is actually the expected behaviour :slight_smile:

lf_clergy       <- leaflet() %>%
                  addMarkers(spdf,
                            lng =   spdf$lon,
                            lat =   spdf$lat,
                            markerClusterOptions())

lf_clergy

Would something like this (see below) work any better?

lf_clergy       <- leaflet() %>%
                  addProviderTiles("Stamen.Toner") %>%
                  addMarkers(spdf,
                            lng =   spdf$lon,
                            lat =   spdf$lat,
                            clusterOptions = markerClusterOptions())

lf_clergy

To elaborate a bit further consider this reproducible example using three NC cities:

library(sf)
library(leaflet)

points <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
                     x = c(-78.633333, -79.819444, -77.912222),
                     y = c(35.766667, 36.08, 34.223333)) %>% 
  st_as_sf(coords = c("x","y"), crs=4326)

# no basemap was specified, and none is shown
leaflet(data = points) %>% 
  addMarkers()


# a basemap was specified, and so is shown
leaflet(data = points) %>%
  addProviderTiles("Stamen.Toner") %>% 
  addMarkers(clusterOptions = markerClusterOptions())

A shameless plug in: a while back I wrote a long form blog post about using {leaflet} in R, including cluster options. In case you find the topic interesting you can have a look here: https://www.jla-data.net/eng/leaflet-in-r-tips-and-tricks/

2 Likes

Thank you, @jlacko , for a quick reply. Base maps are working now, but the marker cluster is still not working. I change my code to use fake data using a library called charlatan, and the same problem happens to the script with the fake data.

I have other codes with the cluster option working, but I cannot figure out why this small script cluster option is not working.

#Library ----
library(dplyr)
library(leaflet)


#Fake Data
library(charlatan)
data <- charlatan::ch_generate('name','job','phone_number', n=5000)

lon <- charlatan::ch_lon(n=5000)
lat <- charlatan::ch_lat(n=5000) 
coords <- dplyr::tibble(lon, lat)

crs    <- CRS("+init=epsg:4326")


spdf <- SpatialPointsDataFrame(coords = coords,
                               data = data,
                               proj4string = crs)

#Leaflet Mapping ----

boxinfo <- paste0('<strong>Name:</strong> ', spdf$name, '<br/><hr>',
                  '<strong>Job:</strong> ', spdf$job, '<br/>',
                  '<strong>Phone Number:</strong> ', spdf$phone_number, '<br/>')


lf_clergy <- leaflet() %>%
  addProviderTiles("Stamen.Toner") %>%
  addMarkers(spdf,
             lng =   spdf$lon,
             lat =   spdf$lat,
             popup = boxinfo,
             markerClusterOptions())

lf_clergy

Thanks for pointing the {charlatan} package out! It will make my reprexes more fun :slight_smile:

You are getting there! You just need to assign the marker options argument by name, not by order. Relying on order of arguments places the markerClusterOptions to icon or what not...

I took the liberty to rewrite your code to a somewhat more concise form; the logic stays the same.

#Library ----
library(dplyr)
library(leaflet)
library(sf)

# Generate Fake Data ---
library(charlatan)
fake_data <- ch_generate('name','job','phone_number', n=5000) %>% 
  mutate(lon = charlatan::ch_lon(n=5000),
         lat = charlatan::ch_lat(n=5000),
         boxinfo = paste0('<strong>Name:</strong> ', name, '<br/><hr>',
                           '<strong>Job:</strong> ', job, '<br/>',
                           '<strong>Phone Number:</strong> ', phone_number, '<br/>')
         )%>% 
  sf::st_as_sf(coords = c("lon", "lat"), crs = 4326)


#Leaflet Mapping ---

lf_clergy <- leaflet(fake_data) %>%
  addProviderTiles("Stamen.Toner") %>%
  addMarkers(popup = ~boxinfo,
             clusterOptions = markerClusterOptions())

lf_clergy

1 Like

Thank you @jlacko it works. :smile:

I appreciated you help me with this problem.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.