leaflet labels overlap fix - leaflet::addMarkers
Asked Answered
W

2

13

I am looking for a fix for overlapping labels when using R function leaflet::addMarkers.

long <- c(147.768, 147.768, 147.768,147.768, 147.768, 147.768)
lat <- c(-36.852, -36.852, -36.852,-36.852, -36.852, -36.852)
label <- c('long label1', 'long label2', 'long label3','long label4', 'long label5', 'long label6')

markers <- data.frame(lat,long,label)


leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup="The birthplace of R",
             label = markers$label,
             labelOptions = labelOptions(noHide = T, direction = 'auto'),
            clusterOptions = markerClusterOptions()
             )
Wichern answered 25/9, 2017 at 15:50 Comment(0)
C
1

You can set the noHide = F instead of noHide = T in labelOptions

And you can try adding options = markerOptions(riseOnHover = TRUE) to get the labels on top of the marker.

Final code would be :

leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=markers$long, lat= markers$lat, 
             popup="The birthplace of R",
             label = markers$label,
             labelOptions = labelOptions(noHide = F, direction = 'auto'),
             options = markerOptions(riseOnHover = TRUE),
             clusterOptions = markerClusterOptions()
             )
Cyclorama answered 24/11, 2017 at 15:54 Comment(1)
I think the OP (and myself) are looking for something that automatically moves labels if they happen to overlap, similar to ggrepel (see blog.revolutionanalytics.com/2016/01/…)Trusteeship
D
1

Unfortunately, I do not believe there is a ggrepel analogy in leaflet.

This does not fix the overlapping labels in the sense of moving them out of the way, but perhaps, it would be an alternative. The idea here is to make the labels less invasive by simply using a reference number for those points. Then, combine the map with a table that has information about those reference numbers. My version of this could use some time to clean up the aestetics, but you get the idea, hopefully.

library(leaflet)
library(crosstalk)
library(DT)

bscols(leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircleMarkers(lng=markers$long, lat= markers$lat, 
             popup="The birthplace of R",
             #label = markers$label,
             #labelOptions = labelOptions(noHide = T, direction = 'top'),
             clusterOptions = markerClusterOptions()) %>%
  addLabelOnlyMarkers(lng=markers$long, lat= markers$lat, 
                      label = as.character(row.names(markers)),
                      labelOptions = labelOptions(noHide = T, textOnly = T),
                      clusterOptions = markerClusterOptions()),
  datatable(markers, width = "100%"))


htmltools::save_html(test, file = "test.html")

image of code output veiwed on microsoft edge in the saved html

Dichromaticism answered 13/1, 2022 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.