stats_noob
stats_noob

Reputation: 5897

Is R displaying points twice?

I have this code for an interactive map in R:

library(leaflet)
library(inlmisc)

Long = rnorm(1000, -71, 0.5)
Lat = rnorm(1000, 42.3, 0.5)

loc = rep("loc", 1000)
Name = rep("Location", 1000)
num = 1:1000
Label = paste0(loc, "_", num)
Location = paste0(Name, "_", num)

df = data.frame(Name, Lat, Long, Label)

map <- leaflet(df) %>%   addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", Name)) %>%
    addTiles() %>%
    setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
    addMarkers(~Long, ~Lat, popup = ~Name, group="marker", label = ~Label) %>% 
    inlmisc::AddSearchButton(group = "marker", zoom = 15,
                             textPlaceholder = "Search here")

The map seems to work fine - but the "icons" are being displayed twice (i.e. blue pins and colored circles, e.g. yellow, green):

enter image description here

Is there a way to have it such that when you zoom out, the blue pins collapse into the colorful circles - and when you zoom in, the colorful circles collapse into the blue pins?

Upvotes: 0

Views: 99

Answers (2)

stats_noob
stats_noob

Reputation: 5897

Using the answer of @Senithil913 - I added a reset option:

map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), 
                popup = ~paste("title: ", Name),
                group="marker", label = ~Label) %>%
    addTiles() %>%
    setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
    inlmisc::AddSearchButton(group = "marker", zoom = 15,
                             textPlaceholder = "Search here") %>%  addResetMapButton() 

Upvotes: 0

Senithil913
Senithil913

Reputation: 46

It looks like you are adding the markers twice.

The first addMarkers line, does the clustering with zoom that it sound like you are interested in.

You can add the group and label options to the first addMarkers call, to be able to search and have mouse-over labels as well as the clustering.

map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
  addMarkers( clusterOptions = markerClusterOptions(), 
              popup = ~paste("title: ", Name),
              group="marker", label = ~Label) %>%
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  inlmisc::AddSearchButton(group = "marker", zoom = 15,
                           textPlaceholder = "Search here")

Upvotes: 1

Related Questions